0

I am trying to convert byte array to string but bytes are not being converted to string correctly.

byte[] testByte = new byte[]
{
    2, 200
};

string string1 = Encoding.ASCII.GetString(testByte);
byte[] byte1 = Encoding.ASCII.GetBytes(string1);

string1 is giving value as \u0002? and byte1 is not getting converted back to 2 and 200. I tried with UTF8 but that is also giving same problem.

I have been given 256 array of chars and integer values. I need to write these values on media as string and read back as bytes. I need conversion to write and read byte data. I am facing problems when integer value comes more then 127.

What should I do so I get original byte values from string?

4
  • 5
    ASCII encoding and UTF8 encoding are intended to encode text, not arbitrary binary data. I believe there are some encodings that will work with the code above, but generally it seems better to use hexadecimal or base64 to encode binary data as text. Can you advise what your goal is? Commented Sep 1, 2021 at 3:03
  • 3
    ASCII only encodes values below 128. Encoding.ASCII converts any values higher than that to ? marks. That's why 200 isn't being encoded/decoded correctly. Commented Sep 1, 2021 at 3:11
  • (And 200 by itself isn't a valid UTF8 character - non-ASCII characters have to be encoded as 2 or more bytes - so you get the same result.) Commented Sep 1, 2021 at 3:16
  • Very unclear what you want to achieve... It is reasonable to assume you've read documentation of the methods you've used so it should not be surprising that sequences of bytes that do not represent valid strings in the given encoding can't be roundtripped. Maybe you are looking for something like hex-encoding (base 16) or more common base64 encoding of bytes into a string? Please edit the question to clarify your actual goal. Commented Sep 1, 2021 at 3:32

2 Answers 2

1

You appear to be using an encoding backwards. A text Encoding(such as ASCII) is for converting arbitrary text data into encoded (meaning: specially formatted) binary data.

(a caveat should be included here that not all encodings support all text characters; ASCII only supports code-points 0-128, for example, but that isn't the main problem with the code shown)

You appear to want to treat arbitrary binary data as a string - which is the exact opposite. Arbitrary binary data, and encoded text data. Not a problem: just use base-N for some N. Hex (base-16) would work, but base-64 will be more space efficient:

string encoded = Convert.ToBase64String(testByte);
byte[] decoded = Convert.FromBase64String(encoded);
Sign up to request clarification or add additional context in comments.

Comments

0

well the problem here is your bite string retrieval chain fix this and then it will fix itself by removing the false elseif! hope this helps

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.