Converting Zoned Decimal fields

I cannot believe I am making this entry… really!  Zoned fields?  Really!

Here it is, 2015!  We’ve just sent a probe past Pluto!  We are mastering the use of stem cells!  We even have flexible flat screens!  And what do I have to deal with?  Zoned fields in a file layout. But I should not be too surprised: the spec I received came as a PDF!  Sheesh, it’s like the 1980’s never ended.

Fortunately, handling these relics of the past isn’t too difficult now. To convert a string with an EBCDIC zoned value in it to an ASCII string with the same numeric value:

var sourceBytes = Encoding.ASCII.GetBytes(sourceString);
var convertedBytes = Encoding.Convert(Encoding.GetEncoding("IBM037"), Encoding.ASCII, sourceBytes);
var convertedString = Encoding.ASCII.GetString(convertedBytes);

And to go the other way just reverse the 1st and 2nd parameters in the call to Convert()


Nuts! That didn’t work. Try this instead:

[edit] The second method I posted also did not work: there are two different Zoned-Decimal schemes (Zoned-Decimal and Zoned-Decimal-Modified – who knew?).  This latest post should handle both.

private static string ConvertZonedToNumeric(string source)
    var pos = "0123456789{ABCDEFGHIpqrstuvwxy}JKLMNOPQR".IndexOf(source.Last());
    var lastDigit = pos % 10;
    var sign = (pos > 19) ? "-" : string.Empty;
    return sign + source.Substring(0, source.Length - 1) + lastDigit; 

To convert the other way, bang your head against a doorjamb and then ask yourself “Why?”. If you think you have a valid reason, bang you head repeatedly against a doorjamb until you can no longer think of a valid reason.

Here’s hoping I never have to look up this entry again.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s