Java中有关byte和String的转换笔记[转] 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。
答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte)0xf0;
byte maskLow = 0x0f; high = (byte)((b & maskHigh) >> 4);
low = (byte)(b & maskLow); StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low)); return buf.toString();
} private static char findHex(byte b) {
int t = new Byte(b).intValue();
t = t < 0 ? t + 16 : t; if ((0 <= t) &&(t <= 9)) {
return (char)(t + '0');
} return (char)(t-10+'A');
} 未解决的疑问在java中不存在类似C中的无符号量,所以如果一个字节超过0x80其对应的整型值即为负值,但在高位右移4位后还是负值,且与对应的正值相差16,比如0xB2经过右移后的期望值是0x0B(11)但实际值是-5与预期的值相差16(这个16通过多次试验得出),对此现象为找到合理的解释。 问题2:“String a=”B2” --> byte b=0xB2”字符的byte转换为byte数据类型
答:思路通过Integer作为转换的中间桥梁
public static int stringToByte(String in, byte[] b) throws Exception {
if (b.length < in.length() / 2) {
throw new Exception("byte array too small");
} int j=0;
StringBuffer buf = new StringBuffer(2);
for (int i=0; i<in.length(); i++, j++) {
buf.insert(0, in.charAt(i));
buf.insert(1, in.charAt(i+1));
int t = Integer.parseInt(buf.toString(),16);
System.out.println("byte hex value:" + t);
b[j] = (byte)t;
i++;
buf.delete(0,2);
} return j;
} 问题3:整数(表示范围限定为两个字节unsigned short)通过Integer.byteValue()转换成byte[2],如果超出一个byte的表示范围将会截断高位的值。
答:思路一个byte能表示的最大整数为256(超过128为负值,超过256将被截断),所以取256的倍数为byte[0],256的余数为byte[1]。
byte[] d = new byte[l+2];
….
buff.put(new Integer(l/256).byteValue());
buff.put(new Integer(l%256).byteValue()); 地址:http://blog.csdn.net/dynamo2/archive/2005/08/30/468105.aspx 评论:管用 自己代码: package test; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map; import org.apache.commons.lang.SerializationUtils; public class SerialTest { public static void main(String... strings) {
// 将map序列化
// 唯一方法,调用方法
Map mp = new HashMap();
mp.put("1", new SeriaTestClass());
mp.put("2", "cde");
byte[] mpbytes = SerializationUtils.serialize((Serializable) mp);
StringBuilder sb = new StringBuilder();
for (byte b : mpbytes) {
sb.append(byteToString(b));
}
String bytestr = sb.toString();
System.out.println(bytestr); // 将bytestr反序列化
// 方法1,直接些
int dtbyteslen = bytestr.length();
byte[] dtbytes = new byte[dtbyteslen / 2];
for (int i = 0; i < dtbyteslen; i += 2) {
String scut = bytestr.substring(i, i + 2);
int tempi = Integer.valueOf(scut, 16);
dtbytes[i / 2] = (byte) tempi;
}
Object o1 = SerializationUtils.deserialize(dtbytes);
System.out.println(o1);
// 方法2,调用方法
byte[] bts = HexString2Bytes(bytestr);
Object o2 = SerializationUtils.deserialize(bts);
System.out.println(o2);
System.out.println(o1 == o2);
System.out.println(o1.equals(o2));
}
private static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
.byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
.byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
private static byte[] HexString2Bytes(String src) {
int len = src.length();
byte[] ret = new byte[len / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < len; i += 2) {
ret[i / 2] = uniteBytes(tmp, tmp[i + 1]);
}
return ret;
}
public static String byteToString(byte b) {
byte high, low;
byte maskHigh = (byte) 0xf0;
byte maskLow = 0x0f; high = (byte) ((b & maskHigh) >> 4);
low = (byte) (b & maskLow); StringBuffer buf = new StringBuffer();
buf.append(findHex(high));
buf.append(findHex(low)); return buf.toString();
} private static char findHex(byte b) {
int t = new Byte(b).intValue();
t = t < 0 ? t + 16 : t; if ((0 <= t) && (t <= 9)) {
return (char) (t + '0');
} return (char) (t - 10 + 'A');
} }
运行结果: ACED0005737200116A6176612E7574696C2E486173684D61700507DAC1C31660D103000246000A6C6F6164466163746F724900097468726573686F6C6478703F4000000000000C77080000001000000002740001327400036364657400013173720013746573742E536572696154657374436C6173737B3039AF33FDCE6C0200024C0001787400134C6A6176612F6C616E672F496E74656765723B4C00017971007E00067870707078
{2=cde, 1=test.SeriaTestClass@b76fa}
{2=cde, 1=test.SeriaTestClass@177b3cd}
false
false 说明:由于内部SeriaTestClass也被序列化,所以两个对象不同 |