zoukankan      html  css  js  c++  java
  • char和byte的转换

    • char转化为byte:

        public static byte[] charToByte(char c) {
            byte[] b = new byte[2];
            b[0] = (byte) ((c & 0xFF00) >> 8);
            b[1] = (byte) (c & 0xFF);
            return b;
        }

    char[]转化为byte[]:

    char[] cChar=new char[5]{a,b,c,d,e};  
    byte[] byteData=Encoding.Default.GetBytes(cChar);  

    // 这样转换,一个2字节的char,只转换为1个byte。

    byte[]转化为char[]:

    byte[] byteData=new byte[5]{0x01,0x02,0x03,0x04,0x05};  
    char[] cChar=Encoding.ASCII.GetChars(byteData);  

    • byte转换为char:

        public static char byteToChar(byte[] b) {
            char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
            return c;
        }

  • 相关阅读:
    背包解法
    第十六周周总结
    软件工程个人课程总结
    学期课后个人总结
    spring事务
    梦断代码03
    团队冲刺的第二十四天
    第十五周周总结
    百度输入法评价
    找到水王
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/1833565.html
Copyright © 2011-2022 走看看