java byte/char/string/int之间的转化

来源:互联网 发布:windows共享文件夹 编辑:程序博客网 时间:2024/05/17 04:49
【公告】关于开启用户注册及登录手机短信验证的通知     CSDN日报20170418 ——《如果两个程序员差不多,选写作能力更好的那个》     程序员4月书讯:Angular来了!

Java中的byte[]/char[]/int/String数据类型转换

标签: java数据二进制编码
1096人阅读 评论(0)收藏举报
本文章已收录于:
分类:
作者同类文章X

    目录(?)[+]

    1. 八种基本数据类型
    2. 各数据之间转化
      1. Stringbyte
        1. 1 Stringbyte
        2. 2 byteString
      2. Stringchar
        1. 1Stringchar
        2. 2charString
      3. Stringint
        1. 1Stringint
        2. 2intString
      4. Stringchar
        1. 1Stringchar
        2. 2charString
      5. Stringbyte
        1. 1Stringbyte
        2. 1byteString
      6. intcharbyte
    3. Java中数据转化的一些规则
    4. 我在进行测试时的代码

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/51743969 文章出自:薛瑄的博客

    你也可以查看我的其他同类文章,也会让你有一定的收货!

    常用的编码方式有Unicode、ASCII、UTF-8、GB2312、ISO-8899-1等。采用不同的编码方式,同样的n位二进制“数字”组合代表的“字符”也会不一样。具体采用什么样的编码方式,对“字符”怎样解析,就要看编程所在的平台是什么样了。同时,为了方便,我们并不会直接用n位二进制的表示,而是用它的十六进制表示。

    八种基本数据类型:

    数据类型名称长度备注byte字节型1字节 = 8bit表示数据范围:-128~127short短整型2字节 = 16bitchar字符型2字节 = 16bit等价于Unicode编码int整型4字节 = 32bitlong长整型8字节float单精度浮点型4字节精度:7-8位double双精度浮点型8字节boolean布尔型true/false

    各数据之间转化:

    1.String<—>byte[]

    1.1 String—>byte[]

    byte[] bytes = mString.getBytes();
    • 1
    • 1

    String默认使用utf-8编码,将mString中的二进制(使用utf-8编码后的二进制),每个字节存储在bytes中。

    例如: mString中的 值:’タ’ 65408 被转为 bytes 中的 -17, -66 ,-128

    65408 对应二进制 1111 1111 1000 0000,这个数据使用utf-8编码后的二进制为:1110 1111 1011 1110 1000 0000,对应- 17,-66, -128

    关于编码,可以查看我的这篇博客

    1.2 byte[]—>String

    //1、默认使用utf-8编码String String1 = new String(bytes);//2、指定其他编码方式String String2 = new String(bytes, StandardCharsets.US_ASCII);String String21 = new String(bytes, StandardCharsets.ISO_8859_1);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 可以指定byte[]—>String使用的编码类型
    • String一个字符是16bit,转化时对byte (8bit)进行符号扩展
    • 使用默认的utf-8编码时,如果byte的二进制数据不符合utf-8编码规范,则转化后 String对应的字符为’�’ 65533

    2.String<—>char[]

    2.1、String—>char[]

    String s = "SSSSSSS";char[] ss = s.toCharArray();
    • 1
    • 2
    • 1
    • 2

    2.2、char[]—>String

     char[] chars4 = {0x0001, 0xfff0, 0xf0, 'a', 0x4E25}; String string4 = new String(chars4);
    • 1
    • 2
    • 1
    • 2
    • char也是使用utf-8编码,如果char的二进制数据不符合utf-8编码规范,用户看到的字符为空。
    • 因为char已经是字符,所以直接把字符赋给String 。

    3.String<—>int

    3.1、String—>int

    String string9 = "严";int i9 = Integer.parseInt(string9);
    • 1
    • 2
    • 1
    • 2

    Exception in thread “main” Java.lang.NumberFormatException: For input string: “严”

    如果String 不是数据,会抛出异常

    3.2、int—>String

    int i = 10;//转化String string9 = String.valueOf(i);//2、直接使用字符串加上i,java会自动转化string9 = "" + i;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    4.String<—>char

    4.1、String—>char

    String string = "严";char char = string.charAt(0);
    • 1
    • 2
    • 1
    • 2

    4.2、char—>String

    char char1 = a;//转化String string9 = String.valueOf(char1);//2、直接使用字符串加上i,java会自动转化string9 = "" + char1;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    5.String<—>byte

    5.1、String—>byte

    可以先转为String—>byte[],在取出其中的byte

    5.1、byte—>String

    byte byte1 = 0xff;//转化String string9 = String.valueOf(byte1);//2、直接使用字符串加上i,java会自动转化,string9 内容为 -1string9 = "" + byte1;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:byte是未经过编码的二进制数据,所以在直接转为String 时,用户看到的是二进制对应的十进制数值

    6.int<—>char<—>byte

    public static void main(String[] args) {  byte b=-1;  System.out.println((int)(char)b);  System.out.println((int)(char)(b & 0xff));}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果是:65535和255

    如果你对输出结果感到很惊讶,请继续往下读…

    1、 int(32位) -> byte(8位)

    -1是int型的字面量,根据“2的补码”编码规则,编码结果为0xffffffff,即32位全部置1.转换成byte类型时,直接截取最后8位,所以byte结果为0xff,对应的十进制值是-1.

    2、 byte(8位) -> char(16位)

    由于byte是有符号类型,所以在转换成char型(16位)时需要进行符号扩展,即在0xff左边连续补上8个1(1是0xff的符号位),结果是0xffff。由于char是无符号类型,所以0xffff表示的十进制数是65535。

    3、 char(16位) -> int(32位)

    由于char是无符号类型,转换成int型时进行零扩展,即在0xffff左边连续补上16个0,结果是0x0000ffff,对应的十进制数是65535。

    4、 int(32位) -> char(16位)

    char c = (char)(b & 0xff);
    • 1
    • 1

    (b & 0xff)的结果是32位的int类型,前24被强制置0,后8位保持不变,然后转换成char型时,直接截取后16位。最后结果为0x00ff

    5、 char 与 int 进行运算

    int i = c & 0xffff;
    • 1
    • 1

    0xffff是int型字面量,所以在进行&操作之前,编译器会自动将c转型成int型,即在c的二进制编码前添加16个0,然后再和0xffff进行&操作,所表达的意图是强制将前16置0,后16位保持不变。虽然这个操作不是必须的,但是明确表达了不进行符号扩展的意图。

    如果需要符号扩展,则可以如下编码:

    int i = (short)c; //Cast causes sign extension
    • 1
    • 1

    首先将c转换成short类型,它和char是 等宽度的,并且是有符号类型,再将short类型转换成int类型时,会自动进行符号扩展,即如果short为负数,则在左边补上16个1,否则补上16个0.

    Java中数据转化的一些规则:

    1. 如果二进制不符合utf-8的规则,在String 中就会被赋值为65533(对应的二进制)
    2. byte是未经编码的二进制数据,在java中可以对byte来进行各种编码。
    3. 在基本数据类型中,存储的都是二进制数据,只是在显示的时候显示各自不同的类型。例如:char保存的二进制数据显示出来是使用编码utf-8后的符号,其它基本数据类型显示出来都是数字。
    4. 基本数据类型转String,方式多样化,

      • 直接相加,
      • String.valueOf()
    5. byte、int、short之间不会互相转换,因为容量小的数据类型会自动转换为大的数据类型,所以在运算时,它们都会被转换为int型

    6. 容量大的数据类型转换成容量小的数据类型时,要加上强制转换符,但可能会造成精度降低或数据溢出
    7. String不属于Java的基本数据类型,String的本质是字符数组,是类对象

    我在进行测试时的代码:

    public class MyClass {    public static void main(String[] args) {        //byte---> string        //byte是未经过编码的原始数据,转为string时可以选择编码格式        byte[] bytes = {0x1f, (byte) 0x7f, (byte) 0xdf80, (byte) 0x80};//        byte[] packetHeard = {(byte)'\u0080',(byte)'\u0001',(byte)'\u007f',(byte)'\u00ff',(byte)'\u00BA',(byte)'\u00CF'};        //1、默认使用utf-8编码        String String1 = new String(bytes);        //2、指定其他编码方式        String String2 = new String(bytes, StandardCharsets.US_ASCII);        String String21 = new String(bytes, StandardCharsets.ISO_8859_1);        //3、使用指定的编码,把bytes转为 charBuffer        Charset charSet = Charset.forName("Unicode");        ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);        byteBuffer.put(bytes);        byteBuffer.flip();        CharBuffer charBuffer = charSet.decode(byteBuffer);        //4、直接使用tostring(),并不能转为字符串        String String3 = bytes.toString();        int i = bytes.hashCode();        String string31 = Integer.toHexString(i);        //5、使用char[] 初始化string,char(显示是utf-8),所以这里直接使用了这些字符        //char、int、short等基本类型的数据,都可以直接用二进制赋值,只是在显示的时候显示各自不同的。例如char显示的是使用编码utf-8后的符号        //而把这些数据类型转化为string时,是根据他们显示的形式加入的stirng        char[] chars4 = {0x0001, 0xfff0, 0x80, 'a', 0x4E25};        short short4 = (short) 0xfff0;        String string4 = new String(chars4);        string4 = String.valueOf(short4);        //6、string  相加,都会保留原来数据属性(数字就是数字,字符就是字符),可以试着把(char)变为(int)等        String string5 = "";        for (i = 0; i < bytes.length; i++) {            string5 += (char) bytes[i];        }        //string-->byte        //7、默认utf-8编码,将string5中的二进制(使用utf-8编码后的二进制),每个字节存储在bytes6中        byte[] bytes6 = string5.getBytes();        //这个所得结果暂时搞不懂        charSet = Charset.forName("ASCII");        bytes6 = string4.getBytes(charSet);        byte[] b = new byte[2];        b[0] = (byte) ((chars4[4] & 0xFF00) >> 8);        b[1] = (byte) (chars4[4] & 0xFF);        //8、        byte b8 = -1;        System.out.println((int) (char) b8);        System.out.println((int) (char) (b8 & 0xff));        //9        String string9 = "严";        int i9 = 0xFFFFFFFF;        string9 = string5 + i9;        char char9 = string9.charAt(0);    }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    参考:

    http://m.blog.csdn.net/article/details?id=47956891
    http://blog.csdn.net/Player26/article/details/3346936
    http://my.oschina.net/joymufeng/blog/139952
    http://www.mytju.com/classcode/tools/encode_utf8.asp
    http://blog.sina.com.cn/s/blog_6047c8870100qftt.html
    http://www.jianshu.com/p/17e771cb34aa
    http://m.blog.csdn.net/article/details?id=49783345

    1
    0
     
     

    我的同类文章

    http://blog.csdn.net
    • Java Lambda表达式入门2016-12-29
    • Android TimerTask 的简单应用2016-11-21
    • 日期操作类(DateFormat与SimpleDateFormat)的区别和使用详解2016-08-15
    • java.util.ConcurrentModificationException原因及解决方法2016-07-22
    • Java 线程池原理和队列详解2016-06-17
    • java中volatile关键字的含义2016-12-12
    • Java中Collections.sort()排序详解2016-08-17
    • Java泛型中的PECS原则2016-08-11
    • Java Socket简单例子、readLine()、readUTF()2016-06-24
    • Java 标准I/O重定向2016-06-16
    更多文章

    参考知识库

    img

    .NET知识库

    img

    软件测试知识库

    img

    Java SE知识库

    img

    Java EE知识库

    img

    Java 知识库

    更多资料请参考:
    猜你在找
    深入Javascript字符串实战视频课程
    实践项目之深入Javascript字符串实战视频课程
    java数据库连接技术JDBC
    顾荣:开源大数据存储系统Alluxio(原Tachyon)的原理分析与案例简介
    Java Swing、JDBC开发桌面级应用
    Java面试题全集上
    Java面试题集锦
    Java面试题全集上
    Java面试题全集上
    java面试题
    关闭
    查看评论

      暂无评论

    发表评论
    • 用 户 名:
    • qq_28260521
    • 评论内容:
    • 插入代码
      HTML/XMLobjective-cDelphiRubyPHPC#C++JavaScriptVisual BasicPythonJavaCSSSQL其它
        
    * 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    快速回复TOP
    核心技术类目
    全部主题HadoopAWS移动游戏JavaAndroidiOSSwift智能硬件DockerOpenStackVPNSparkERPIE10EclipseCRMJavaScript数据库UbuntuNFCWAPjQueryBIHTML5SpringApache.NETAPIHTMLSDKIISFedoraXMLLBSUnitySplashtopUMLcomponentsWindows MobileRailsQEMUKDECassandraCloudStackFTCcoremailOPhoneCouchBase云计算iOS6RackspaceWeb AppSpringSideMaemoCompuware大数据aptechPerlTornadoRubyHibernateThinkPHPHBasePureSolrAngularCloud FoundryRedisScalaDjangoBootstrap
    0 0
    原创粉丝点击