C# 문자열 16진수 변환

-- C# 2013. 7. 8. 16:38
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

public string ConvertStringToHex(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;
}

public string ConvertHexToString(string HexValue)
{
    string StrValue = "";
    while (HexValue.Length > 0)
    {
        StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
        HexValue = HexValue.Substring(2, HexValue.Length - 2);
    }

    return StrValue;
}

 

추가) 2013.08.08

위의 예처럼 2자리씩 끊어 변환하는 것이 아니라 Hex전체를 10진수로 변환하기

public static string ConvertHexAllToString(string HexValue)
{
    string StrValue = "";

    if(HexValue.Length > 0)
    {
        StrValue = System.Convert.ToUInt64(HexValue, 16).ToString();
    }

    
    return StrValue;
}

출처 : http://snipplr.com/view/36461/

posted by 어린왕자악꿍