1

so i am trying to add two of 0x00 after every string i write in a binary file, these are what i tried(1 try per line), but i always end up having only one 0x00 after each string:

bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + '\0' + '\0'));
bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + "\0"));
bw.Write(enc.GetBytes(listView1.Items[i].SubItems[1].Text + (new string('\0', 2))));

but they all end up with the same result, is there another Unicode escape i can try to use ? or am i doing anything wrong in these lines ? and for an aside note, String enc = Encoding.Unicode

1
  • 1
    Focus on the code that reads this file. It is bound to ignore the extra zeros, it is pretty common for such code to assume C string semantics. Where a single 0 terminates the string. Particularly so since such code could not use any other way to find out how many characters are in the string. Use a hex viewer to gain confidence in your code. Commented Jul 23, 2013 at 10:50

1 Answer 1

1

What you are doing should work. This test code demonstrates:

string test = "X" + '\0' + '\0';
Console.WriteLine(test.Length); // Prints 3
var bytes = Encoding.Unicode.GetBytes(test);
Console.WriteLine(bytes.Length); // Prints 6

bytes contains the following byte values: 88, 0, 0, 0, 0, 0 - which is correct.

So I assume there's something wrong elsewhere with your code.

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

1 Comment

Yes, you are right, thank you, i know now where i was wrong, i don't really know how this works in SOF, so ill just post your answer and the Answer, thank you :)

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.