I cannot reliably write the Hex number "83" to a stream

A

ajmastrean

I cannot get any (hex) number in the "0x80"-"0x89" range (inclusive)
to write properly to a file. Any number in this range magically
transforms itself into "0x3F". For instance, debugging shows that
"0x83" = UInt16 "131" and that converts to Char (curly) "f". Any
information would be helpful.

String[] hexNum = { "79", "80", "89", "90" };
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:
\test.dbf", true, System.Text.Encoding.Default);

foreach (String hex in hexNum)
{
Char hexChar = (Char)UInt16.Parse(hex,
System.Globalization.NumberStyles.HexNumber);
streamWriter.Write(hexChar);
}

streamWriter.Flush();
streamWriter.Close();
 
L

Lasse Vågsæther Karlsen

ajmastrean said:
I cannot get any (hex) number in the "0x80"-"0x89" range (inclusive)
to write properly to a file. Any number in this range magically
transforms itself into "0x3F". For instance, debugging shows that
"0x83" = UInt16 "131" and that converts to Char (curly) "f". Any
information would be helpful.

String[] hexNum = { "79", "80", "89", "90" };
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:
\test.dbf", true, System.Text.Encoding.Default);

foreach (String hex in hexNum)
{
Char hexChar = (Char)UInt16.Parse(hex,
System.Globalization.NumberStyles.HexNumber);
streamWriter.Write(hexChar);
}

streamWriter.Flush();
streamWriter.Close();

Have you looked at the default encoding and what it does to your data?
Since you're writing text characters through an encoding object, it will
in some cases encode the characters different than if you just output
pure bytes to begin with.
 
B

Ben Voigt [C++ MVP]

ajmastrean said:
I cannot get any (hex) number in the "0x80"-"0x89" range (inclusive)
to write properly to a file. Any number in this range magically
transforms itself into "0x3F". For instance, debugging shows that
"0x83" = UInt16 "131" and that converts to Char (curly) "f". Any
information would be helpful.

String[] hexNum = { "79", "80", "89", "90" };
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(@"C:
\test.dbf", true, System.Text.Encoding.Default);

The only thing the "default" encoding is good for is sending to another .NET
program that also uses the default encoding. You're giving it permission to
store the data in the file any way it chooses.

If you need a particular encoding, then set the encoding to what you need.

Or use byte instead of char, bytes do not undergo encoding transformations.
 
J

Jon Skeet [C# MVP]

ajmastrean said:
I cannot get any (hex) number in the "0x80"-"0x89" range (inclusive)
to write properly to a file.

You're not writing numbers. You're writing characters. That's what a
StreamWriter is for. If you want to write straight bytes, you should
just use a Stream (eg FileStream).

Do you really want to write Unicode characters 0x80 to 0x89 to a file?
Bear in mind they're unlikely to be in the default encoding...
 
C

christery

Check UTF-8, antyhting over 127 is not ASCII, its utf encoded...
thats why is seems silly...

//CY
 
Top