convert byte[] to string

convert byte[] to string

Ask Question

10 years, 6 months ago

10 years, 6 months ago

4k times




How do you convert a byte array to a string? I need to get the raw content, e.g. “96=A8=FC-=A8=FE”, but when I use say Encoding.UTF8.GetString(bytes), it returns “96��-��”. Thanks!

c# .net

share|improve this question

edited Sep 16 ’09 at 21:53


asked Sep 15 ’09 at 14:38


1,01522 gold badges1010 silver badges1414 bronze badges


There’s really no such this as a string without an encoding: Remember ASCII is still an encoding!

– stusmith
Sep 15 ’09 at 14:44

yah you are right stushmith! there’s no such thing as “no encoding” .. my bad ..

– ban-G
Sep 16 ’09 at 21:53

add a comment

4 Answers





I think you misunderstand the content of strings. The closest you’ll get to “raw content” is to use Encoding.Unicode – .NET uses UTF-16 internally, so converting to UTF-16 is effectively just a case of copying the contents of memory from the string to the byte array.

Now, to come back to your problem, what data do you have, what is it meant to represent and why? Textual data is characters. Binary data is numbers, basically. You have to have a mapping between the two, and that’s the encoding.

I have an article on Unicode which may help you, but I strongly suspect you’ll need to take a step back before you make any progress.

If you’re trying to convert a byte array into a string representation of those bytes as hex, you can just use BitConverter.ToString(byte[]) but I wouldn’t describe that as a “raw” conversion.

EDIT: Okay, now that we have the context, it’s much easier to answer. What you’re looking at is quoted printable encoding. The email should specify the encoding of the quoted printable, so when you decode the QP encoding, that’s what you should use. If you’re not currently storing the content encoding of the original email, you should start doing so right now…

share|improve this answer

edited Sep 15 ’09 at 15:31

answered Sep 15 ’09 at 14:43

Jon SkeetJon Skeet

1172k746746 gold badges83898389 silver badges87178717 bronze badges

I think you’re right to ask him to take a step back and reconsider the question. My best guess, though, is that he expects these encoded hex values to translate to characters that are viewable on his machine. If so, perhaps he needs to map from some local code page to Unicode.

– Steven Sudit
Sep 15 ’09 at 14:49


That’s just Encoding.Default, if that’s really what he expects 🙂

– Jon Skeet
Sep 15 ’09 at 15:03

What I need to do is parse the contents of an email. Before parsing we save the contents in the database. That column is returned to me in byte[]. My main problem is when the email contains fractions, e.g. “96⅜-⅞”. The fraction part is converted in to hexadecimal but the rest of the message is the same. I already have a code that will take care of the conversion (using regular expression) but I don’t know how to get “96=A8=FC-=A8=FE” from the returned column, which is in byte[]. Maybe my approach to solving this problem is unsatisfactory. Any suggestions would be appreciated. Thanks!

– ban-G
Sep 15 ’09 at 15:10

Just to complicate Jon’s answer, not only do you need to check for the content-encoding being QP, you need to check for what charset to interpret it as. ASCII 0-127 is more or less consistent, but whether 0xA8 maps to a fraction character depends entirely on the charset. Frankly, this is not a trivial problem to solve; you need a deeper understanding of RFC 2822 and everything it touches. May I suggest giving up and using a decent library, like Chilkat’s S/MIME decoder?

– Steven Sudit
Sep 16 ’09 at 4:46

Thanks again Jon Skeet for your input and for sharing the link to your article about unicode and .net — it was really helpful!

– ban-G
Sep 16 ’09 at 22:00

show 1 more comment


You effectively want a formatted string of the hexadecimal representation of each bytes. The question How do you convert Byte Array to Hexadecimal String, and vice versa, in C#? will show you how to get the string, and you can alter that code to add any “in-between-bytes” formatting you want.

share|improve this answer

edited May 23 ’17 at 10:09


111 silver badge

answered Sep 15 ’09 at 14:42

Adam WrightAdam Wright

46.3k1010 gold badges125125 silver badges146146 bronze badges

Thanks Adam! This was also helpful.

– ban-G
Sep 16 ’09 at 21:59

add a comment


You probably want to use the ASCII encoding, rather than the UTF8.

share|improve this answer

answered Sep 15 ’09 at 14:40

John GietzenJohn Gietzen

44.1k2828 gold badges133133 silver badges181181 bronze badges

add a comment


You can also use the Convert.ToBase64CharArray Method

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

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

share|improve this answer

answered Sep 15 ’09 at 14:45


1,73333 gold badges1515 silver badges2626 bronze badges

add a comment

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password


Post as a guest


Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged c# .net or ask your own question.

The Overflow Blog

Podcast Episode 220: Fully Remote

Tracking down performance pitfalls in Vue.js

Featured on Meta

Planned maintenance scheduled for Saturday, March 28, 2020 at 13:00 UTC (9AM…

An Update On Creative Commons Licensing

Community and Moderator guidelines for escalating issues via new response…

How does the Triage queue work?

Triage needs to be fixed urgently, and users need to be notified upon…



How do you convert a byte array to a hexadecimal string, and vice versa?


6420What is the difference between String and string in C#?1350How do you convert a byte array to a hexadecimal string, and vice versa?338Can a Byte[] Array be written to a file in C#?2869Case insensitive ‘Contains(string)’2174How do I get a consistent byte representation of strings in C# without manually specifying an encoding?919How to convert UTF-8 byte[] to string?657Split a string by another string in C#639Converting string to byte array in C#1359Why not inherit from List<T>?

Hot Network Questions

Is travelling with 1 friend permitted in the UK now that the lockdown is enforced?

Does lower gravity on Mars make it unsafe and unhealthy for humans?

Confusion about the definition of the uncertainty principle

Has radio astronomy ever been done on objects that appear very close to the Moon? Is this avoided?

Is the so called kTC noise, that is inherent in an RC filter, dependent on bandwidth?

What are the objections to Mitch McConnell’s COVID-19 Bill?

Why John 1:1 in DRB is not literal translation from the Latin Vulgate?

Can I create an image with a specific size in bytes?

Ranged-attack initiative and sequencing for monsters and NPCs

Are Putin’s constitution amendments legal?

When I burn isopropyl alcohol (IPA), it burns orange. But when I burn ethyl alcohol, it burns totally blue. Why is this?

Is there a trick to help kids know which direction pawns are going?

How can an omniscient being have limited awareness?

Pendulum Encoding

Is there a tool that will make a rounded cut in flat metal wire?

Why would a ‘first world’ alien opt to have a brood of dumb children rather than one smart one?

How do you cross-breed flowers?

How to pass a reference to a template typename argument

Which mobile game featuring a red-roof cartoon house is this?

How does linear phase shift of bessel filter avoid distortion of nonsinusoidal inputs?

What’s the point of Super Star Destroyers?

What should I call a “differential” which cubes, rather than squares, to zero?

Does including a sport commentator voice in a music track make me owe him mechanical royalties?

Why is paragliding not an old invention?

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


Leave a Reply

Your email address will not be published. Required fields are marked *