3

The following converts a string into a byte array:

byte[] sent_byte = new byte[cmd_str.length];
for (int i = 0; i < sent_byte.length; i++) {
    sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
}

How do you convert it back to the original string?

Maljam
  • 6,244
  • 3
  • 17
  • 30
Sandah Aung
  • 6,156
  • 15
  • 56
  • 98

5 Answers5

2

You have to create a new String object and assign the byte array to it:

String stringBytes = new String(sent_byte);
Alexander Ortiz
  • 529
  • 7
  • 14
  • will not work. Please try at least compiling and running the code from the question before upping the answer. cmd_str is array of strings **each** of which he is trying to convert to exactly **1** byte. – iantonuk Apr 05 '16 at 11:01
1

You could also make use of:

Integer.toString( (int)sent_byte[i], 16 );
Maljam
  • 6,244
  • 3
  • 17
  • 30
0
public static String encode(byte[] byteArray) {
    StringBuffer hexBuffer = new StringBuffer(byteArray.length * 2);
    for (int i = 0; i < byteArray.length; i++)
        for (int j = 1; j >= 0; j--)
            hexBuffer.append(HEX[(byteArray[i] >> (j * 4)) & 0xF]);
    return hexBuffer.toString();
}
Taha Naqvi
  • 1,756
  • 14
  • 24
0

The answer to your question: impossible with standard java tools.

Explanation: from this code

Integer.parseInt(cmd_str[i], 16)

We understand that cmd_str[] is array of Strings, because

static int  parseInt(String s)
static int  parseInt(String s, int radix)

is a method to parse String(not byte, or byte array) into signed Integer.

and what happens in this code

(byte) Integer.parseInt...

Is that this 32 bits of signed integer are being cast to 8bits signed integer. which means if you try to

String bits8string = new String(sent_byte, "AnyStandardCharset");

you will get garbage(or nothing), because chars are not signed integers. Therefore conversion from int to char is not some charset(like ascii) table, because it makes no sense to map characters to negative values.(I have never seen any such table.

here code assembled from yours as a proof:

import java.io.UnsupportedEncodingException;


public class stringbyte16{

public static void main(String cmd_str[]) throws UnsupportedEncodingException{
    byte[] sent_byte = new byte[cmd_str.length];
    String bits8string="";
    for (int i = 0; i < sent_byte.length; i++) {
        sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
        System.out.println(sent_byte[i]);
    }
    bits8string+=new String(sent_byte, "UTF-16");//or "US-ASCII", nothing, etc
    System.out.println(bits8string);
    }
}  

Just run it with hex numbers separated by space(as intended in question)

The only way to fix this is smth like this:

import java.io.UnsupportedEncodingException;


public class stringbyte16{


public static void main(String cmd_str[]) throws UnsupportedEncodingException{


    Character HexChar [] = {'0', '1', '2', '3', '4', '5', '6', '7',
                  '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

byte[] sent_byte = new byte[cmd_str.length];
String bits8string="";
for (int i = 0; i < sent_byte.length; i++) {
    sent_byte[i] = (byte) Integer.parseInt(cmd_str[i], 16);
    System.out.println(sent_byte[i]);
    bits8string+=HexChar[(int)sent_byte[i]];        
}

System.out.println(bits8string);


    }
}
iantonuk
  • 1,178
  • 8
  • 28
-1

Try this:

 public static void main(String args[]) throws UnsupportedEncodingException {
            String str = "0123456abcdef中文123abc";

            byte[] data = str.getBytes("UTF-8");
            String hex = encode(data, 0, data.length);
            System.out.println(hex);

            data = decode(hex);
            str = new String(data, "UTF-8");
            System.out.println(str);

            hex += "a";
            data = decode(hex);
            str = new String(data, "UTF-8");
            System.out.println(str);
        }