java 进制转换

String转byte[]:

1 public byte[] strToBytes(String str) {
2     if (str == null) {
3         return null;
4     }
5     byte[] bytes = str.getBytes();
6     return bytes;
7 }

看源码知道getBytes() 处理:

 1 /**
 2  * JDK source code
 3  */
 4 public byte[] getBytes(Charset charset) {
 5     String canonicalCharsetName = charset.name();
 6     if (canonicalCharsetName.equals("UTF-8")) {
 7         return Charsets.toUtf8Bytes(value, offset, count);
 8     } else if (canonicalCharsetName.equals("ISO-8859-1")) {
 9         return Charsets.toIsoLatin1Bytes(value, offset, count);
10     } else if (canonicalCharsetName.equals("US-ASCII")) {
11         return Charsets.toAsciiBytes(value, offset, count);
12     } else if (canonicalCharsetName.equals("UTF-16BE")) {
13         return Charsets.toBigEndianUtf16Bytes(value, offset, count);
14     } else {
15         CharBuffer chars = CharBuffer.wrap(this.value, this.offset, this.count);
16         ByteBuffer buffer = charset.encode(chars.asReadOnlyBuffer());
17         byte[] bytes = new byte[buffer.limit()];
18         buffer.get(bytes);
19         return bytes;
20     }
21 }

上述代码其实就是根据给定的编码方式进行编码。如果调用的是不带参数的getBytes()方法,则使用默认的编码方式:

 1 /**
 2  * JDK source code
 3  */
 4 private static Charset getDefaultCharset() {
 5     String encoding = System.getProperty("file.encoding", "UTF-8");
 6     try {
 7         return Charset.forName(encoding);
 8     } catch (UnsupportedCharsetException e) {
 9         return Charset.forName("UTF-8");
10     }
11 }

默认编码方式是由System类的"file.encoding"属性决定的,经过测试,在简体中文Windows操作系统下,默认编码方式为"GBK",在Android平台上,默认编码方式为"UTF-8"

byte[]转String:

1 public String bytesToStr(byte[] bytes) {
2     if (bytes == null || bytes.length == 0) {
3         return null;
4     }
5     String str = new String(bytes);
6     return str;
7 }

注意的是Java中String类的数据是Unicode类型的,因此上述的getBytes()方法是把Unicode类型转化为指定编码方式的byte数组;而这里的Charset为读取该byte数组时所使用的编码方式.

 因为一个byte是八位,可以用两个十六进制位来表示,因此,byte数组中的每个元素可以转换为两个十六进制形式的char,所以最终的HexString的长度是byte数组长度的两倍。

将字符串,转换成十六进制字符串。

 1 /**   
 2  * 字符串转换成十六进制字符串  
 3  * @param String str 待转换的ASCII字符串  
 4  * @return String 如: [616C6B]  
 5  */      
 6 public static String str2HexStr(String str)    
 7 {      
 8   
 9     char[] chars = "0123456789ABCDEF".toCharArray();      
10     StringBuilder sb = new StringBuilder("");    
11     byte[] bs = str.getBytes();      
12     int bit;      
13         
14     for (int i = 0; i < bs.length; i++)    
15     {      
16         bit = (bs[i] & 0x0f0) >> 4;      
17         sb.append(chars[bit]);      
18         bit = bs[i] & 0x0f;      
19         sb.append(chars[bit]);
20     }      
21     return sb.toString().trim();      
22 }

 将十六进制字符串 转换成 byte[]

 1 /**  
 2  * 十六进制字符串转换为Byte值  
 3  * @param String 十六进制字符串,每个Byte之间没有分隔符  
 4  * @return byte[]  
 5  */    
 6 public static byte[] hexStr2Bytes(String src)    
 7 {
 8     int m=0,n=0;    
 9     int l=src.length()/2;  
10     byte[] ret = new byte[l];    
11     for (int i = 0; i < l; i++) {    
13         m=i*2+1;    
14         n=m+1;    
15         ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n));    
16     }    
17     return ret;    
18 } 

 

byte[] 与String 转换出乱码问题

string与十六进制byte互转

Android蓝牙开发之数据窜位和数据接收错误以及重组字节数据

 

posted @ 2017-07-19 16:31  晕菜一员  阅读(320)  评论(0编辑  收藏  举报