title: How to read EBCDIC data with a non standard codepage, and not mess up numbers?
body: <p>Here is one for the old(er) hands :-)</p>
  
  <p>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.</p>
  
  <p>So I open the file with a streamreader using System.Text.Encoding like so:</p>
  
  <pre><code>Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(20424)
  Dim sr As New StreamReader(item.Key, encoding)
  </code></pre>
  
  <p>and proceed to read the VARCHAR and CHAR data according to their lengths into char arrays using</p>
  
  <pre><code>sr.ReadBlock(buffer, 0, iFieldBufferSize)
  </code></pre>
  
  <p>Always remembering the first 2 bytes in a VARCHAR column should be discarded
  and getting the correct string with</p>
  
  <pre><code>SringValue = encoding.GetString(encoding.GetBytes(buffer))
  </code></pre>
  
  <p>And all is Great!</p>
  
  <p>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</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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)!</p>
  
  <p>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!!!</p>
  
  <p>Bottom line, the non stadard codepage encoder messes up the byte readings!!!</p>
  
  <p>Any idea how to get around this?</p>