2

I'm reading metadata from specific bytes in files but the results I get have no encoding. I would like to encode them to Encoding.Default for readability.

How do I convert either full Unicode string or at least a single char?

C# .NET 3.5

5 Answers 5

4

You don't encode bytes to a particular encoding - you decode them from the original encoding. You have to use the right encoding to use - you can't just pick it arbitrarily.

Do these bytes actually represent text data? If so, what encoding do the already use? This should be part of the file format.

If they're not actually encoded text, but you want a reliable text representation of arbitrary binary data, use Convert.ToBase64String.

Sign up to request clarification or add additional context in comments.

Comments

2
var myString = System.Text.Encoding.Unicode.GetString(myByteArray);

Would that work?

1 Comment

Probably not, utf-16 isn't very likely. Could happen though.
1

Have a look at the namespace System.Text.Encoding :)

Comments

0

use somthing like this

System.Text.Encoding.Default.GetChars(pass your byte array here)

Comments

0

It will be something like this:

string s = Encoding.UTF8.GetString(bytesToGetStringFrom, 0, bytesToGetStringFrom.Length);

There are "encodings" other than UTF8.

1 Comment

Noticing Jon's comment, it is decoding, which is why "encoding" is in quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.