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
The following program has worked for converting an EBCDIC value to an integer, when receiving data from one of our customers. The data we get may be a subset of what you might get, so see if this works for you:
using System;
using System.Text;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
string strAmount = "00007570{";
Console.WriteLine("{0} is {1}", strAmount, ConvertEBCDICtoInt(strAmount));
strAmount = "000033}";
Console.WriteLine("{0} is {1}", strAmount, ConvertEBCDICtoInt(strAmount));
Console.ReadLine();
}
// This converts "00007570{" into "75700", and "000033}" into "-330"
public static int? ConvertEBCDICtoInt(string i_strAmount)
{
int? nAmount = null;
if (string.IsNullOrEmpty(i_strAmount))
return(nAmount);
StringBuilder strAmount = new StringBuilder(i_strAmount);
if (i_strAmount.IndexOfAny(new char[] { '}', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R' }) >= 0)
strAmount.Insert(0, "-");
strAmount.Replace("{", "0");
strAmount.Replace("}", "0");
strAmount.Replace("A", "1");
strAmount.Replace("J", "1");
strAmount.Replace("B", "2");
strAmount.Replace("K", "2");
strAmount.Replace("C", "3");
strAmount.Replace("L", "3");
strAmount.Replace("D", "4");
strAmount.Replace("M", "4");
strAmount.Replace("E", "5");
strAmount.Replace("N", "5");
strAmount.Replace("F", "6");
strAmount.Replace("O", "6");
strAmount.Replace("G", "7");
strAmount.Replace("P", "7");
strAmount.Replace("H", "8");
strAmount.Replace("Q", "8");
strAmount.Replace("I", "9");
strAmount.Replace("R", "9");
// Convert the amount to a int:
int n;
if (int.TryParse(strAmount.ToString(), out n))
nAmount = n;
return (nAmount);
}
}
}