Showing posts with label Hex. Show all posts
Showing posts with label Hex. Show all posts

Wednesday, December 19, 2012

C# Convert Ascii string to Hex string vice versa

Convert Ascii to Hex method
 
public string ConvertAsciiToHex(string asciiString)
{
    string hex = "";
    foreach (char c in asciiString)
    {
       int tmp = c;
       hex += String.Format("{0:X2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}
Convert Hex to Ascii method
 
public string ConvertHextoAscii(string HexString)
{
    string asciiString = "";
    for (int i = 0; i < HexString.Length; i += 2)
    {
       if (HexString.Length >= i + 2)
       {
          String hs = HexString.Substring(i, 2);
          asciiString = asciiString + System.Convert.ToChar(System.Convert.ToUInt32(HexString.Substring(i, 2), 16)).ToString();
       }
    }
    return asciiString;
}

C# Convert Hex to Decimal and vice versa

Convert Hex to Decimal as below;
 
int DecimalVal = Convert.ToInt32(HexVal, 16);
Convert Decimal to Hex as below;
 
string HexVal = DecimalVal .ToString("X");