How to Convert Java String into Byte[]

How to convert Java String into byte[]?

The object your method decompressGZIP() needs is a byte[].

So the basic, technical answer to the question you have asked is:

byte[] b = string.getBytes();
byte[] b = string.getBytes(Charset.forName("UTF-8"));
byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only

However the problem you appear to be wrestling with is that this doesn't display very well. Calling toString() will just give you the default Object.toString() which is the class name + memory address. In your result [B@38ee9f13, the [B means byte[] and 38ee9f13 is the memory address, separated by an @.

For display purposes you can use:

Arrays.toString(bytes);

But this will just display as a sequence of comma-separated integers, which may or may not be what you want.

To get a readable String back from a byte[], use:

String string = new String(byte[] bytes, Charset charset);

The reason the Charset version is favoured, is that all String objects in Java are stored internally as UTF-16. When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String, depending upon the chosen charset.

Convert String to byte[] array

can you please show the exact Exception or Error which is occurring in the console. because it works completely fine with me.

byte b1[] = new byte[256];
String s = "hello there";
b1 = s.getBytes();
System.out.println(b1);

String to byte in java

You can do something like:

byte hexaByte = Byte.parseByte("1A", 16);

where 16 is radix (base of system numeration), but if the number is higer than 127 it will cause NumberFormatException.

String to byte[] conversion issue

Arrays.toString(byte[]) doesn't just convert the byte[] into a String, it converts it to a human-readable format. When you then call getBytes() on that String, it is converting the characters that represent the original byte information into a byte[], along with the formatting characters, such as the brackets and commas.

If you want to create a String from a byte[] use the String constructor which takes a byte[] to explicitly create a String object containing your data:

    ...
//byte[] to string
String byte_string = new String(byte_array);

//String to byte[]
byte[] string_byte = byte_string.getBytes();

System.out.println(Arrays.equals(byte_array, string_byte));

As pointed out by others, not all binary data is cleanly represented in all character sets, so you might be able to get the conversion to work by explicitly specifying the encoding.

For instance, the above sample code still outputs false when I try to encode an executable program file (.exe), but compares as true if I specify ISO_8859_1 encoding:

    //byte[] to string
String byte_string = new String(byte_array, StandardCharsets.ISO_8859_1);

//String to byte[]
byte[] string_byte = byte_string.getBytes(StandardCharsets.ISO_8859_1);

System.out.println(Arrays.equals(byte_array, string_byte));

The absolute safest way to convert your data to a String and back would be to use base64 encoding as suggested by this answer:

    //file to byte[] 
byte[] byte_array = Files.readAllBytes(path);
byte[] encoded = Base64.encodeBase64(byte_array);

//byte[] to string
String byte_string = new String(encoded, StandardCharsets.US_ASCII);

//String to byte[]
byte[] string_byte = byte_string.getBytes(StandardCharsets.US_ASCII);
byte[] decoded = Base64.decodeBase64(string_byte);

System.out.println(Arrays.equals(byte_array, decoded));

How to put a String to a byte array in Java without conversion

You can get a substring of two chars of your string like this:

String yourString = "07df";
String firstByteAsString = yourString.substring(0,2); // start - end(excl.)
// = "07"

You can get a byte from this like this:

byte b = Byte.parseByte( firstByteAsString, 16 );

I am sure, you'll manage to use this to get your desired functionality.

EDIT
As Peter points out: Values > 127 are a Problem. So you'll actually have to use

byte b = (byte) (Integer.parseInt( firstByteAsString, 16) & 0xFF);

But his answer is much simpler and doing all this for you. You should go for BigInteger and accept Peter's answer.



Related Topics



Leave a reply



Submit