3

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 i.e. b[6] from my byte array outputs 0x3F, but it should be 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 String encoding output:

byte0: 72
byte1: 3a
byte2: 60
byte3: 1
byte4: e
byte5: 10
byte6: ffffffef
byte7: ffffffbf
1
  • What text character is 0x8A supposed to represent, and in what encoding? It's not ASCII, as previously stated here, and it does not appear to be ISO-8859-1 either. htmlhelp.com/reference/charset/latin1.gif Commented Nov 20, 2012 at 7:05

4 Answers 4

5

Try another form of the String constructor:

String bitmapStr = new String(b,"ISO-8859-1");
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't work - I still get an odd result in byte 6. 8A is of course the only byte value where the most significant bit is set, so I assume the sign is affecting the conversion.
Updated my answer per your findings. Thanks.
No matter what kind of constructor he uses, he is casting a value as a byte which is outside of the byte range. Even if you use the constructor from this answer, you end up with a negative value because of the byte cast.
Also, I don't think 0x8A is a valid character in ISO-8859-1. htmlhelp.com/reference/charset/latin1.gif
2

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");

Comments

1

You are forcing hexadecimal become byte ( 8 bits ). This is called casting and you cannot do that. You notice that all values have a good output except when 0xYZ where Y >=8 ! do not use casting unless you're sure you're not gonna lose information.

1 Comment

Err, what? Hexadecimal is simply another representation of the data, of course it's valid to cast from hex to byte, they are a different representation of the same thing! When Y>=8 issues occur because ASCII encoding supports up to 7 bits, (the 7th bit is 0x80).
0

Following the suggestions from Rahul and irreputabe I figured it out:

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:

int byte1 = b[i];

So I just added:

int byte1 = b[i] & 0xFF;

for the correct result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.