private static int CharToInt(char c)
if ('0' <= c && c <= '9')
if ('a' <= c && c <= 'f')
throw new ArgumentOutOfRangeException(nameof(c));
static string IntToCharMap = "0123456789abcdef";
private static char IntToChar(int i)
public static string Add(string str1, string str2)
var result = new char[Math.Max(str1.Length, str2.Length) + 1];
for (int i = 0; i < result.Length; i++)
int i1 = str1.Length - i - 1;
int i2 = str2.Length - i - 1;
int n1 = (0 <= i1) ? CharToInt(str1[i1]) : 0;
int n2 = (0 <= i2) ? CharToInt(str2[i2]) : 0;
int sum = n1 + n2 + overflow;
result[result.Length - i - 1] = IntToChar(sum % 16);
return new String(result, 1, result.Length - 1);
return new String(result);
public static void Main()
Console.WriteLine(Hex.Add("fed", "13"));