我需要将ASCII转换为十六进制值。请参考ASCII表格,但下面列出了一些示例:
- ascii 1 = 31
- 2 = 32
- 3 = 33
- 4 = 34
- 5 = 35
- A = 41
- a = 61 等等
测试一下
string input = "12345";
string hex = string.Join(string.Empty,
input.Select(c => ((int)c).ToString("X")).ToArray());
Console.WriteLine(hex);
string input = "12345";
// Replace 1252 with whatever code page you're using, if not that one
string hex = string.Join(string.Empty,
Encoding.GetEncoding(1252).GetBytes(input).Select(b => b.ToString("X")).ToArray());
Console.WriteLine(hex);
将字符转换为ASCII码
int c = (int)'a';
类似于Anthony Pegram的解决方案,但更加LINQ风格且稍微短一些,但由于在aggregate方法中存在多个字符串分配,因此速度较慢。
string hex = input.Select(c => ((int)c).ToString("X")).Aggregate((a, s) => a + s);
尝试使用这个方法
public static string AsciiToHexadecimal(List<int> asciiList)
{
return asciiList.Aggregate("", (current, i) => current + $"{i:X}");
}
这是一个方法,可以帮助您将ASCII数字数组转换为十六进制。