By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();
Jan 16 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ajmastrean wrote:
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jan 16 '08 #2

P: n/a

"ajmastrean" <aj********@gmail.comwrote in message
news:67**********************************@z17g2000 hsg.googlegroups.com...
>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.
>
foreach (String hex in hexNum)
{
Char hexChar = (Char)UInt16.Parse(hex,
System.Globalization.NumberStyles.HexNumber);
streamWriter.Write(hexChar);
}

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

Jan 16 '08 #3

P: n/a
ajmastrean <aj********@gmail.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 16 '08 #4

P: n/a
Check UTF-8, antyhting over 127 is not ASCII, its utf encoded...
thats why is seems silly...

//CY
Jan 20 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.