700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java integer long 转换_Java中的类型转换(Integer Long String)

java integer long 转换_Java中的类型转换(Integer Long String)

时间:2020-11-24 22:47:16

相关推荐

java integer long 转换_Java中的类型转换(Integer Long String)

这段时间将项目中一个模块参照C++源代码,实现一个JAVA版。主要功能是将一些字段信息转换为String类型,传输后可以进行解析。

Integer、Long转为String,Java本身提供了这种转换方法。

Example:

int a = 127;

String s = new String(a);

System.out.println(s);

//输出 127

而我的项目需求是Integer、Long字段在String类型中所占大小为固定自己。比如Integer类型的127在内存中应该为0x7f,0x00,0x00,0x00(Little Endian),在String类型输出可能为乱码,但需要在解析后可以还原为Integer类型的127。

C++中存在无符号数,而JAVA中没有无符号数。所以以byte[]替代unsigned char[]会出现小问题。见下:

int n = 0xff7f0012;

byte[] b = new byte[4];

b[3] = (byte) (n & 0xff);

b[2] = (byte) (n >> 8 & 0xff);

b[1] = (byte) (n >> 16 & 0xff);

b[0] = (byte) (n >> 24 & 0xff);

String s = new String(b);

for (byte i : b) {

System.out.printf("%02x ", i);

}

System.out.println();

System.out.println(s.length());

for (byte i : s.getBytes()) {

System.out.printf("%02x ", i);

}

//输出如下

ff 7f 00 12

3

3f 00 12

由上可见,byte[]转换String类型时,会发生问题,应为byte表示的范围是-128~127,当byte[]中元素大于127时,会被转换成63。尝试使用不同的编码方案解决。

测试发现UTF-8,GBK,US-ASCII均未得到正确结果。尝试charset为8859_1得到正确结果。

将上面程序中的String s = new String(b);替换为下面程序段:

String s = "";try{

s= new String(b, "8859_1");

}catch(UnsupportedEncodingException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

即可得到正确结果,下面是程序功能的简单封装。

public class Test{

private static byte[] intToByte(int n) {

byte[] b = new byte[4];

b[3] = (byte) (n & 0xff);

b[2] = (byte) (n >> 8 & 0xff);

b[1] = (byte) (n >> 16 & 0xff);

b[0] = (byte) (n >> 24 & 0xff);

return b;

}

private static byte[] longToByte(long n) {

byte[] b = new byte[8];

b[7] = (byte) (n & 0xff);

b[6] = (byte) (n >> 8 & 0xff);

b[5] = (byte) (n >> 16 & 0xff);

b[4] = (byte) (n >> 24 & 0xff);

b[3] = (byte) (n >> 32 & 0xff);

b[2] = (byte) (n >> 40 & 0xff);

b[1] = (byte) (n >> 48 & 0xff);

b[0] = (byte) (n >> 56 & 0xff);

return b;

}

private static int byteToInt(byte[] b, int beginPos) {

return b[beginPos + 3] & 0xff |

(b[beginPos + 2] & 0xff) << 8 |

(b[beginPos + 1] & 0xff) << 16 |

(b[beginPos] & 0xff) << 24;

}

private static int byteToLong(byte[] b, int beginPos) {

return b[beginPos + 7] & 0xff |

(b[beginPos + 6] & 0xff) << 8 |

(b[beginPos + 5] & 0xff) << 16 |

(b[beginPos + 4] & 0xff) << 24 |

(b[beginPos + 3] & 0xff) << 32 |

(b[beginPos + 2] & 0xff) << 40 |

(b[beginPos + 1] & 0xff) << 48 |

(b[beginPos] & 0xff) << 56;

}

private static String intToString(int n) {

byte[] b = intToByte(n);

String s = "";

try {

s = new String(b, "8859_1");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return new String(s);

}

private static String longToString(long n) {

byte[] b = longToByte(n);

String s = "";

try {

s = new String(b, "8859_1");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return new String(s);

}

private static int stringToInt(String str) {

byte[] b = new byte[4];

try {

b = str.getBytes("8859_1");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return byteToInt(b, 0);

}

private static int stringToLong(String str) {

byte[] b = new byte[8];

try {

b = str.getBytes("8859_1");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return byteToLong(b, 0);

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。