2

I have a string that represents a byte

string s = "\x00af";

I write this string to a file so the file contains the literal "\x00af" and not the byte it represents, later I read this string from the file, how can I now treat this string as byte again (and not the literal)?

Here is a sample code:

public static void StringAndBytes()
{
    string s = "\x00af";
    byte[] b = Encoding.ASCII.GetBytes(s);

    // Length would be 1
    Console.WriteLine(b.Length);

    // Write this to a file as literal
    StreamWriter sw = new StreamWriter("c:\\temp\\MyTry.txt");
    sw.WriteLine("\\x00af");
    sw.Close();

    // Read it from the file
    StreamReader sr = new StreamReader("c:\\temp\\MyTry.txt");
    s = sr.ReadLine();
    sr.Close();

    // Get the bytes and Length would be 6, as it treat the string as string
    // and not the byte it represents
    b = Encoding.ASCII.GetBytes(s);
    Console.WriteLine(b.Length);
}

Any idea on how I convert the string from being a text to the string representing a byte? Thx!

2
  • Don't write it like that. You don't want to have to write a C# compiler to read it back. And never store bytes in a string, not all byte values are legal Unicode characters. Use Convert.ToBase64String to encode byte[] into a string that you can easily write to a text file. Commented Mar 3, 2011 at 1:53
  • 1
    When questions are answer you can mark them as such ;) Commented Mar 3, 2011 at 13:02

4 Answers 4

1

Is it a requirement for the file content to have the string literal? If no, then you might want to write the byte[] b array directly to the file. That way when you read it, it is exactly, what you wrote.

byte[] b = Encoding.UTF32.GetBytes(s);
File.WriteAllBytes ("c:\\temp\\MyTry.txt", b);

b = File.ReadAllBytes ("c:\\temp\\MyTry.txt");
s = Encoding.UTF32.GetString (b);

If you need the file content to have the string literal, while being able to convert it to the original text written, you will have to choose the right encoding. I believe UTF32 to be the best.

    b = new byte[4];
    b[0] = Byte.Parse(s.Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier);
    string v = Encoding.UTF32.GetString(b);
    string w = "\x00af";

    if (v != w)
        MessageBox.Show("Diff [" + w + "] = [" + v + "] ");
    else
        MessageBox.Show("Same");
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! your second example was exactly what I wanted. The file have to have the literal (what Im doing actually is parsing .cs files looking for those special strings and manipulate them).
1

Not sure if I understand the question correctly, but you're not writing the string s to the file. You have an extra \ in your WriteLine statement! WriteLine("\\x00af") writes the characters \, x, 0, 0, a and f, since the first \ acts as an escape to the second one ...

Did you mean

sw.WriteLine("\x00af");

or

sw.WriteLine(s);

instead? This works as expected in my tests.

Comments

0

Use the Encoding.ASCII.GetString(byte[]) method. It is also available from all the others encoding. Make sure you always use the same encoding to decode the byte[] as you used to encode it or you won't get the same value every time.

Here's an example.

Comments

0

Just parse a string representing each byte:

Byte b = Byte.Parse(s);

1 Comment

Yes, but doesn't parse "\x00af".

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.