Writing a hex value to a stream

Hello Everyone,

I am trying to hex value to a stream. What is the easiest way of doing it

I tried the following and a lot more but some reason I can not get it to work
BinaryWriter bw = new BinaryWriter(fs);
bw.Write("\uc080");

I want the c080 to appear exactly i.e it should be 0xc0 0x80.

But the value seems to be getting converted to the dec value.

So what would be the best way of doing this

Thanks in advance


Answer this question

Writing a hex value to a stream

  • Stampede2

    Dave,
    try this:

    bw.Write(Encoding.Unicode.GetBytes ("\uc080"));

    HTH
    --mc


  • Deepthi Rao

    Or this, if you don't want to risk encoding problems:

    FileStream fs = new FileStream(@"c:\temp\test.bin", FileMode.Create);
    fs.Write(BitConverter.GetBytes(0xC080), 0, 2);
    fs.Close();

    Beware that output is little-endian so 0x80 appears first in the file...


  • Writing a hex value to a stream