简体   繁体   English

Java字符串到字节转换问题

[英]Java String to byte conversion issue

I have some test code which is not working as I expect, after reviewing various sites and specifications I still cannot figure out what is happening. 我有一些测试代码没有按照我的预期工作,在审查了各种网站和规范后,我仍然无法弄清楚发生了什么。

Here is my test code: 这是我的测试代码:

byte[] b = new byte[8];
b[0] = (byte)0x72;
b[1] = (byte)0x3A;
b[2] = (byte)0x60;
b[3] = (byte)0x01;
b[4] = (byte)0x0E;
b[5] = (byte)0x10;
b[6] = (byte)0x8A;
b[7] = (byte)0x11;
String bitmapStr = new String(b);
try {
    b = bitmapStr.getBytes("US-ASCII");
} catch (Exception ex) {
    ex.printStackTrace();
}
System.out.println("DEBUG: bitmapStr = \"" +bitmapStr + "\"");
for (int i=0; i<=7; i++) {
    int byte1 = b[i];
    System.out.println("byte"+i + ": " + Integer.toHexString(byte1));
}

When I run the program I get the following in the console output: 当我运行程序时,我在控制台输出中得到以下内容:

DEBUG: bitmapStr = "r:`�"
byte0: 72
byte1: 3a
byte2: 60
byte3: 1
byte4: e
byte5: 10
byte6: 3f
byte7: 11

See how byte6 ie b[6] from my byte array outputs 0x3F, but it should be 0x8A. 看看byte6即b [6]如何从我的字节数组输出0x3F,但它应该是0x8A。

Any ideas why? 有什么想法吗?

By the way, if I use UTF-8 encoding I get an even more funky output (although ASCII is correct). 顺便说一句,如果我使用UTF-8编码,我会得到一个更加时髦的输出(虽然ASCII是正确的)。

UTF-8 String encoding output: UTF-8字符串编码输出:

byte0: 72
byte1: 3a
byte2: 60
byte3: 1
byte4: e
byte5: 10
byte6: ffffffef
byte7: ffffffbf

尝试另一种形式的String构造函数:

String bitmapStr = new String(b,"ISO-8859-1");

Try something like this to change a string to byte:- 尝试这样的方法将字符串更改为字节: -

  String source = "2675326";
 byte[] byteArray = source.getBytes("UTF-16LE");

or change your code to:- 或将您的代码更改为: -

 String bitmapStr = new String(b,"US-ASCII");

You are forcing hexadecimal become byte ( 8 bits ). 您强制十六进制变为字节(8位)。 This is called casting and you cannot do that. 这称为铸造,你不能这样做。 You notice that all values have a good output except when 0xYZ where Y >=8 ! 您注意到所有值都有一个良好的输出, except when 0xYZ where Y >=8 ! do not use casting unless you're sure you're not gonna lose information. 除非你确定你不会丢失信息,否则不要使用铸造。

Following the suggestions from Rahul and irreputabe I figured it out: 根据Rahul的建议和不可原谅我明白了:

When I changed to UTF-16LE / ISO-8859-1 encoding, byte6 was output as: "ffffff8a", then I realised I was performing a type conversion from byte to int here: 当我改为UTF-16LE / ISO-8859-1编码时,byte6输出为:“ffffff8a”,然后我意识到我在这里执行从byte到int的类型转换:

int byte1 = b[i];

So I just added: 所以我刚补充说:

int byte1 = b[i] & 0xFF;

for the correct result. 为了正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM