String for char array java

I'm at a dead end and I need fresh eyes, I'm not sure why this code does this.

String string = new String(new char[] {(char) 0x01, (char) 0x02, ... ,(char) 0xFC}); 

The output is all that should be up to the last number (0xFC), it returns -4, I know its hexadecimal value, but if I do the same with 252 decimal value, it also gives me a negative result. Hope this is just a solution and I just don't see it.

Thanks in advance.

+46
java arrays
Apr 6 '12 at 20:30
source share
1 answer

String to char array is as simple as

 String str = "someString"; char[] charArray = str.toCharArray(); 

Can you explain a little more about what you are trying to do?

* Update *

if I understand your new comment, you can use an array of bytes and provide an example.

 byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array(); for (byte b : bytes) { System.out.format("0x%x ", b); } 

With the next exit

0x65 0x10 0xf3 0x29

+135
Apr 6 2018-12-12T00:
source share



All Articles