I working with some EBCDIC data that I need to parse and find some Hex values. The problem that I’m having is that it appears that I’m reading the file in with the incorrect encoding. I can see that my record begins with ‘!‘ (which is a x5A in EBCDIC) but when doing the conversion to hex it returns as a x21, which is the ASCII value for a ‘!‘.
I was hoping that there was a built-in method in the framework, but I’m afraid that I’m going to have to create a custom class to correctly map the EBCDIC character set.
Using fileInStream As New FileStream(inputFile, FileMode.Open, FileAccess.Read) Using bufferedInStream As New BufferedStream(fileInStream) Using reader As New StreamReader(bufferedInStream, Encoding.GetEncoding(37)) While Not reader.EndOfStream Do While reader.Peek() >= 0 Dim charArray(52) As Char reader.Read(charArray, 0, charArray.Length) For Each letter As Char In charArray Dim value As Integer = Convert.ToInt16(letter) Dim hexOut As String = [String].Format('{0:x}', value) Debug.WriteLine(hexOut) Next Loop End While End Using End Using End Using
Thanks!
Yes, when you read the text data in as strings, it’s storing it internally as Unicode. If you care about the binary values (i.e. the raw bytes) then don’t decode it in the first place.
If you really need to do anything with a custom EBCDIC encoding, you can use my open source EBCDIC implementation – but I think you really just need to make up your mind as to whether you’re treating this as binary data or text.