Convert byte [] to string

How do you convert a byte array to a string? I need to get raw content for example. "96 = A8 = FC- = A8 = FE" but when i use say Encoding.UTF8.GetString (bytes) it returns "96 -". Thank you!

+2


source to share


4 answers


I think you misunderstood the contents of the lines. The closest you'll get to "raw content" is to Encoding.Unicode

use-.NET uses UTF-16 internally, so converting to UTF-16 is actually just a case of copying the memory contents from a string to a byte array.

Now to get back to your problem, what data do you have, what does it mean to represent, and why? Text data is symbols. Binary data is basically numbers. You must have a mapping between them and that encoding.

I have an article on Unicode that might help you, but I strongly suspect that you need to take a step back before you progress.



If you are trying to convert an array of bytes to the string representation of those bytes as hex, you can just use BitConverter.ToString(byte[])

, but I would not describe it as a "raw" conversion.

EDIT: Ok, now that we have the context, it's much easier to answer. What you are looking for is quoted printable encoding. The email should include the encoding of the quoted print, so when you decode the QP encoding this is what you should be using. If you are not currently preserving the encoding of the original email content, you should start doing so now ...

+10


source


You really want a formatted string of hexadecimal representation of each byte. Question : How to convert a byte array to a hexadecimal string, and vice versa, in C #? will show you how to get a string and you can modify that code to add whatever "between bytes" formatting you want.



+2


source


You probably want to use ASCII encoding and not UTF8.

+1


source


You can also use Convert.ToBase64CharArray method

http://msdn.microsoft.com/en-us/library/system.convert.tobase64chararray(VS.80).aspx

Convert.ToBase64CharArray (Byte [], Int32, Int32, Char [], Int32)

Converts a subset of an unsigned 8-bit integer array to the equivalent subset of a base 64-digit Unicode character array. parameters specify subsets as offsets in the input and output arrays, and the number of elements in the input array to convert.

0


source







All Articles