Your byte array must have some encoding. The encoding cannot be ASCII if you've got negative values. Once you figure that out, you can convert a set of bytes to a String using:
byte[] bytes = {...}
String str = new String(bytes, StandardCharsets.UTF_8); // for UTF-8 encoding
There are a bunch of encodings you can use, look at the supported encodings in the Oracle javadocs.
Answer from omerkudat on Stack OverflowYour byte array must have some encoding. The encoding cannot be ASCII if you've got negative values. Once you figure that out, you can convert a set of bytes to a String using:
byte[] bytes = {...}
String str = new String(bytes, StandardCharsets.UTF_8); // for UTF-8 encoding
There are a bunch of encodings you can use, look at the supported encodings in the Oracle javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
If you really must use a String to hold binary data then the safest way is to use Base64 encoding.
How do you convert an "Byte array to string" text array back to normal text?
Convert byte array to string
If you're trying to create a String from a C-style char* string, the string has to be null-terminated. If the final element of byteArray is not 0, then the program does not know how long your string is and will overrun into whatever follows it in memory.
Sometimes what follows may be a 0 and the string terminates correctly, other times it will be followed by random data that is then appended to the string.
You haven't told the String constructor how long byteArray is, so how would it know how long to make strData? The answer is that it copies data until it reaches that terminating null character.
edit
Unfortunately the String methods which accept a length argument are protected. Try something like this, where you iterate through each element and append it individually:
String strData; for (char c : byteArray) strData += c;More on reddit.com
python - How to convert a byte array to string? - Stack Overflow
c# - How to convert byte array to string - Stack Overflow
I got a string called abc, I got the bytes of abc and put them into the byte array named b, then i created a string which converts byte array b to string via Arrays.toString(b). This results in [97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 104, 116].
How would you reverse this? (it's a seperate function).
I am trying to convert byte array to string. It is working fine for some cases but doesnt work with others.
String strData=String ((char*)byteArray);
strData has an extra char at the end. But sometimes it doesnt has that char and working perfectly fine. There is no extra char in the byte array. I double Checked.
Any help would be appreciated. Thanks
Edit: I think there is a problem with my program design. I need to create a lookup table. I need to read values from addresses against the strings. The Strings will come from c# application in the form of byte array. I am trying to convert byte array to String.
If you're trying to create a String from a C-style char* string, the string has to be null-terminated. If the final element of byteArray is not 0, then the program does not know how long your string is and will overrun into whatever follows it in memory.
Sometimes what follows may be a 0 and the string terminates correctly, other times it will be followed by random data that is then appended to the string.
You haven't told the String constructor how long byteArray is, so how would it know how long to make strData? The answer is that it copies data until it reaches that terminating null character.
edit
Unfortunately the String methods which accept a length argument are protected. Try something like this, where you iterate through each element and append it individually:
String strData;
for (char c : byteArray) strData += c;
is the extra char a null byte or ??
I don't know what String() does internally, but some C++ char array functions will add a null terminator byte and others don't.
Depending on the encoding you wish to use:
var str = System.Text.Encoding.Default.GetString(result);
Assuming that you are using UTF-8 encoding:
string convert = "This is the string to be converted";
// From string to byte array
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(convert);
// From byte array to string
string s = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length);