How to convert from EBCDIC to ASCII in C#.net

后端 未结 7 1193
时光取名叫无心
时光取名叫无心 2020-12-06 06:57

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 tha

7条回答
  •  [愿得一人]
    2020-12-06 07:12

    These are the extension methods and unit test that we use:

        /// 
        /// parses a signed or unsigned decimal in EBCDIC format int an integer
        /// 
        /// 
        /// 
        private static int? FromZonedDecimalString(this string value)
        {
            var trimmed = ("" + value).Trim();
            if (trimmed.Length == 0)
                return null;
    
            int testValue;
            if (Int32.TryParse(trimmed, out testValue))
                return testValue;
    
            var lastChar = Convert.ToChar(trimmed.Substring(trimmed.Length - 1, 1));
            var result = 0;
    
            if (trimmed.Length > 1)
                result = Int32.Parse(trimmed.Substring(0, trimmed.Length - 1)) * 10;
    
            switch (lastChar)
            {
                case '{':
                    return result;
                case '}':
                    return -1 * result;
                default:
                    if (lastChar >= 'A' && lastChar <= 'I')
                        return result + lastChar - 'A' + 1;
                    if (lastChar >= 'J' && lastChar <= 'R')
                        return (result + lastChar - 'J' + 1) * -1;
                    if (lastChar >= '0' && lastChar <= '9')
                        return (result + lastChar - '0' + 1) * -1;
                    break;
            }
            return null;
        }
    
        /// 
        /// converts an integer value into zoned signed EBCDIC decimal format
        /// 
        /// 
        /// 
        public static string ToZonedSignedDecimalString(this int value)
        {
            var str = Math.Abs(value).ToString();
            str = str.Substring(0, str.Length - 1);
            var lastDigit = Math.Abs(value % 10);
    
            if (value < 0)
            {
                if (lastDigit == 0) return str + "}";
                if (lastDigit == 1) return str + "J";
                if (lastDigit == 2) return str + "K";
                if (lastDigit == 3) return str + "L";
                if (lastDigit == 4) return str + "M";
                if (lastDigit == 5) return str + "N";
                if (lastDigit == 6) return str + "O";
                if (lastDigit == 7) return str + "P";
                if (lastDigit == 8) return str + "Q";
                if (lastDigit == 9) return str + "R";
    
                throw new NotSupportedException("If this throws, I'm at a loss. Last digit was: " + lastDigit);
            }
    
            if (lastDigit == 0) return str + "{";
            if (lastDigit == 1) return str + "A";
            if (lastDigit == 2) return str + "B";
            if (lastDigit == 3) return str + "C";
            if (lastDigit == 4) return str + "D";
            if (lastDigit == 5) return str + "E";
            if (lastDigit == 6) return str + "F";
            if (lastDigit == 7) return str + "G";
            if (lastDigit == 8) return str + "H";
            if (lastDigit == 9) return str + "I";
    
            throw new NotSupportedException("If this throws, I'm at a loss. Last digit was: " + lastDigit);
        }
    
    
    [TestClass]
    public class IntExtensionsTests
    {
        [TestMethod]
        public void TestConversion()
        {
            string signedDecimalString;
            int convertedlValue;
            for (int i = -1000001; i <= 1000001; i++)
            {
                signedDecimalString = i.ToZonedSignedDecimalString();
                convertedlValue = signedDecimalString.ConvertRightSignedJustifySignedValueToInt();
    
                Assert.AreEqual(i, convertedlValue);
            }
        }
    }
    

提交回复
热议问题