zoukankan      html  css  js  c++  java
  • Oracle的函数vsize和length的区别讨论

    The "length" functions return the length of char. LENGTH calculates length using characters as defined by the input character set.

    LENGTHB uses bytes instead of characters. LENGTHC uses Unicode complete characters. LENGTH2 uses UCS2 codepoints. LENGTH4 uses UCS4 codepoints

    length函数返回字符的长度,它使用定义好的输入的字符集计算长度.
    lengthb使用bytes代替字符

    VSIZE returns the number of bytes in the internal representation of expr.

    vsize 返回内部表示的字节的数目。internal representation of expr谁能解释一下。

    看sql示例:

    select length('adfad合理') "bytesLengthIs" from dual --7

    select lengthb('adfad') "bytesLengthIs" from dual --5

    select lengthb('adfad合理') "bytesLengthIs" from dual --11

    select vsize('adfad合理') "bytesLengthIs" from dual --11

    select lengthc('adfad合理')"bytesLengthIs" from dual --7

    结论:在utf-8的字符集下
    lengthb=vsize
    lengthc=length

    疑问:中文字符怎么会占用了3个byte?而不是2个。是utf-8字符集的原因?
    谁知道??????

    参考文献:Oracle9i SQL Reference Release 2 (9.2)
     

    ..................................................

    用String的getBytes方法测试了一下.
    结论是utf-8的中文字符占用3个字节,gbk的中文字符占用2个字节,iso-8859-1的中文字符被识别为占用2个字节,iso不支持中文字符的编码,应该是都当成某个拉丁字母了.Oracle没有关系,oracle只是负责存储数据.
    可以先用 select * from v$nls_parameters 看看oracle的字符集
    下边是测试的类:

    import java.io.UnsupportedEncodingException;

    public class TextEncoding
    {

    /**
    *
    * @author:sunflower
    * @date: 2007-1-24 上午10:09:40
    * @todo: 调用的是String的自己的getBytes(encoding)方法,
    * 使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中.
    * @param content
    * @param encode
    * @return
    */
    public static byte[] getBytes(String content,String charsetName)
    throws UnsupportedEncodingException{
    return content.getBytes(charsetName);
    }

    /**
    *
    * @author:sunflower
    * @date: 2007-1-24 上午10:19:40
    * @todo: 调用的是String的自己的getBytes()方法,
    * 使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
    * @param content
    * @return
    */
    public static byte[] getBytes(String content){
    return content.getBytes();
    }

    public static void main(String[]args){
    String content="1e宝宝";
    byte[] len;
    try{
    len=getBytes(content,"UTF-8");
    System.out.println(" the byte array length is "+len.length);
    len=getBytes(content,"GBK");
    System.out.println(" the byte array length is "+len.length);
    len=getBytes(content,"ISO-8859-1");
    System.out.println(" the byte array length is "+len.length);
    }catch(Exception e){
    System.out.println("Can 't recognize");
    }

    // System.out.println("the content byte[] length is "+);
    }

    }

    输出 :
    the byte array length is 8
    the byte array length is 6
    the byte array length is 4


    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1492768

  • 相关阅读:
    什么叫继承?
    两类交换元素使序列有序 求最少交换次数的题
    如何理解汉诺塔
    求给出第 K个 N位二进制数,该二进制数不得有相邻的“1”
    7/26 CSU-ACM2018暑期训练3-递归&递推-选讲
    平面分割
    递推算法之平面分割问题总结
    UTC时间
    7/25 CSU-ACM2018暑假集训比赛1
    洛谷 P1824 进击的奶牛【二分答案/类似青蛙过河】
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/1779011.html
Copyright © 2011-2022 走看看