Here is one for the old(er) hands :-)
I'm reading a binary dump from a mainframe DB2 table. The table has varchar, char, smallint, integer and float columns. To make it interesting, the DB2 uses code page 424 (Hebrew). I need my code to be codepage independent.
So I open the file with a streamreader using System.Text.Encoding like so:
Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(20424)
Dim sr As New StreamReader(item.Key, encoding)
and proceed to read the VARCHAR and CHAR data according to their lengths into char arrays using
sr.ReadBlock(buffer, 0, iFieldBufferSize)
Always remembering the first 2 bytes in a VARCHAR column should be discarded and getting the correct string with
SringValue = encoding.GetString(encoding.GetBytes(buffer))
And all is Great!
But now i get to the SMALLINT column, and i'm in trouble. The value of the signed number is stored in 2 bytes, and because its Big endian, i do
Dim buffer(iFieldBufferSize - 1) As Byte
buffer(1) = sr.Read ''switch the bytes around!
buffer(0) = sr.Read
Dim byteBuffer(iFieldBufferSize - 1) As Byte
Dim i16 As Int16 = BitConverter.ToUInt16(buffer, 0)
and i get wrong numbers! for example, if the bytes are 00 03 i get 0 in buffer(1) and 3 in buffer(0) - good. BUT when the two bytes are 00 20, i get 128 read into buffer(0)!
So after half a day of pulling my hair, i drop the encoder from the streamreader declaration, and now i'm getting 32 read into buffer(0), like it should be!!!
Bottom line, the non stadard codepage encoder messes up the byte readings!!!
Any idea how to get around this?