Unexpected results when writing char[]

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I am attempting to use a BinaryWriter to write a char array containing the
following 6 hex values:

0x7e
0x86
0x6f
0x7a
0x6c
0x41

The code I'm using looks like this:

FileStream fs = File.Create(MyFile);
BinaryWriter w = new BinaryWriter((Stream) fs, Encoding.ASCII);
w.Write(myCharArray, 0, myCharArray.Length);
w.Flush();
fs.Close();

All of the characters are written correctly to the file except for the
second (0x86) which comes out as 0x3F. I have verified that the characters
in the array are definitely the values shown above. The same code works
fine on a desktop. Does anyone know what's going wrong?

Thanks...

Dan
 
This is because ASCII encoding does not have any characters above 0x7f
defined. 0x3f is '?' - default substitute character.
Instead of Encoding.ASCII try using Encoding.GetEncoding(1251)
 
I get a "PlatformNot Supported" exception on the following line:

BinaryWriter w = new BinaryWriter((Stream) fs,
Encoding.GetEncoding("1251"));
 
I then changed to the line below, which didn't raise the exception, but
produced the same output as before. I also don't understand why the
original code works on the desktop, but does not work in the Compact
framework.

BinaryWriter w = new BinaryWriter((Stream) fs, Encoding.GetEncoding(1251));
 
Ok. It was actually 1252, not 1251, but that is not the real problem. The
real problem is that character 0x86 exists only in unicode (and UTF). What
you are trying to do is to write non-existent character to the output. This
is why it is replaced by ?. Unlike C characters that are simply bytes, .NET
characters are special type. All conversions of this type are governed by
encoding tables. If your value is not in the encoding table, it will be
replaced. It seems that what you actually need is to write a byte array
where each byte is a result of direct cast of the character value to byte
value
Writer.Write((byte)arr )
 
Thanks Alex. That appears to work. .NET is a very cool thing, but this
seems like a long way to go just to write a bunch of bytes that happen to
start out as chars...Dan


Alex Feinman said:
Ok. It was actually 1252, not 1251, but that is not the real problem. The
real problem is that character 0x86 exists only in unicode (and UTF). What
you are trying to do is to write non-existent character to the output. This
is why it is replaced by ?. Unlike C characters that are simply bytes, ..NET
characters are special type. All conversions of this type are governed by
encoding tables. If your value is not in the encoding table, it will be
replaced. It seems that what you actually need is to write a byte array
where each byte is a result of direct cast of the character value to byte
value
Writer.Write((byte)arr )


--
Alex Feinman
---
Visit http://www.opennetcf.org
Dan said:
I get a "PlatformNot Supported" exception on the following line:

BinaryWriter w = new BinaryWriter((Stream) fs,
Encoding.GetEncoding("1251"));
 
If you need to write some bytes, you might want to declare them as Byte[]
and use binary writer.

It won't offer unwanted "services" like this.



As a matter of fact, these "services" are indeed annoying sometimes.

Most non-English speakers probably encountered bunch of '?' instead of
native characters in e-mail passed vie Exchange.

I would prefer "don't know what to do with character - keep it is as it is"
rather than "let's replace it with '?', it will make a good charade".



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and confers no rights.


Dan said:
Thanks Alex. That appears to work. .NET is a very cool thing, but this
seems like a long way to go just to write a bunch of bytes that happen to
start out as chars...Dan


Alex Feinman said:
Ok. It was actually 1252, not 1251, but that is not the real problem. The
real problem is that character 0x86 exists only in unicode (and UTF).
What
you are trying to do is to write non-existent character to the output. This
is why it is replaced by ?. Unlike C characters that are simply bytes, .NET
characters are special type. All conversions of this type are governed by
encoding tables. If your value is not in the encoding table, it will be
replaced. It seems that what you actually need is to write a byte array
where each byte is a result of direct cast of the character value to byte
value
Writer.Write((byte)arr )


--
Alex Feinman
---
Visit http://www.opennetcf.org
Dan said:
I get a "PlatformNot Supported" exception on the following line:

BinaryWriter w = new BinaryWriter((Stream) fs,
Encoding.GetEncoding("1251"));


This is because ASCII encoding does not have any characters above 0x7f
defined. 0x3f is '?' - default substitute character.
Instead of Encoding.ASCII try using Encoding.GetEncoding(1251)

--
Alex Feinman
---
Visit http://www.opennetcf.org
I am attempting to use a BinaryWriter to write a char array
containing
the
following 6 hex values:

0x7e
0x86
0x6f
0x7a
0x6c
0x41

The code I'm using looks like this:

FileStream fs = File.Create(MyFile);
BinaryWriter w = new BinaryWriter((Stream) fs, Encoding.ASCII);
w.Write(myCharArray, 0, myCharArray.Length);
w.Flush();
fs.Close();

All of the characters are written correctly to the file except for the
second (0x86) which comes out as 0x3F. I have verified that the
characters
in the array are definitely the values shown above. The same code
works
fine on a desktop. Does anyone know what's going wrong?

Thanks...

Dan

 
Back
Top