I have a value in EBCDIC format "000000{". I want to convert it into a.Net Int32 type. Can anyone let me know what I can do about it?? So my question is given a string that contains a signed numeric in EBCDIC , what should I be doing to convert it into a .NET Int32.
Thanks so much in advance!
Try following function..
This question is quite old, but we recently ran into the same issue. It seems that some large financial institutions (I'm looking at you Fidelity) still use old-school mainframe systems that you need to communicate with and these systems expect zoned-decimal.
The problem that I have found with other answers is that they use string manipulation operations, which are slow. I put together a simple C# library that does the conversion numerically and dropped it on GitHub. Check the link below for an in-depth description of the issue. I have included the (current as of now) implementation of the ZonedDecimalConverter class.
zoned-decimal on GitHub
These are the extension methods and unit test that we use:
Generally speaking, you should be able to load EBCDIC data using the correct System.Text.Encoding class (the link points to a list of all encodings, which includes EBCDIC encodings). The string is then Unicode in memory and can be saved to ASCII using the ASCII encoding.
This does what you ask for in the title of the question. However, I'm not sure whether this is what you wanted to know, since your question isn't fully clear to me. If you're looking for the ASCII character code, you can just cast the character to an
int
as long as they are ASCII characters only.You're going to want to read up on binary-coded decimals, as that is what you are facing, and there are questions to answer before you can really code it.
If the value is a single character, it may be as simple as getting the char number--but you need to know if the system is Big Endian (like most mainframes from which you would be getting EBDIC-encoded files) or Little Endian (like more modern OSes).
If your integer value uses more than one character and includes the sign (as you mention), then it is more complex. Most likely, each half (or "nibble", or 4 bits) of each character represents the number--maybe 0 thru 9 or in hex 0 thru F, and the string is padded with zeros (nulls, actually) on the left, and the last nibble contains the sign. This system might be called Zoned Decimal in some parlance.
All in all, I would recommend starting by reading this article, which should introduce you to how data is/was stored on COBOL-based mainframes, and get you moving the right direction.
In C#, you may be able to do the conversion form the common Zoned Decimal (which sounds like the best fit for your incoming data as you have described it) by using int.Parse with the correct NumberStyles options, like this:
Try this