String to char array Java

JavaArrays

Java Problem Overview


I am stumped on this and I need some fresh eyes, I'm not sure why this code is doing this.

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

The output is everything it should be up until the last number (the 0xFC) it returns a -4, I know its a hex value, but if I do the same with 252 the decimal value, it gives me a negative as well. I hope this is just a simple solution, and I just can't see it.

Thanks ahead of time.

Java Solutions


Solution 1 - Java

A string to char array is as simple as

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

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

*** Update ***

if I am understanding your new comment, you can use a byte array and example is provided.

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

With the following output

0x65 0x10 0xf3 0x29

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCody KeasberryView Question on Stackoverflow
Solution 1 - JavaKevinView Answer on Stackoverflow