需要将ascii值转换为hex值

发布于 2024-08-30 07:59:25 字数 265 浏览 3 评论 0原文

我需要将 ascii 值转换为十六进制值。请参阅 Ascii 表,但我在下面列出了一些示例:

  • ascii 1 = 31
  • 2 = 32
  • 3 = 33
  • 4 = 34
  • 5 = 35
  • A = 41
  • a = 61 等

但我使用 int 而不是字符串值。是否可以这样做。 因此int测试=12345; 需要得到转换后的i = 3132333435

I need to convert ascii to hex values. Refer to the Ascii table but I have a few examples listed below:

  • ascii 1 = 31
  • 2 = 32
  • 3 = 33
  • 4 = 34
  • 5 = 35
  • A = 41
  • a = 61 etc

But I am using int instead of string values. Is it possible to do that.
Therefore int test = 12345;
Need to get the converted i = 3132333435

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山田美奈子 2024-09-06 07:59:25

测试此

string input = "12345";
string hex = string.Join(string.Empty,
    input.Select(c => ((int)c).ToString("X")).ToArray());

Console.WriteLine(hex);

注意:在 C# 4 中,不需要调用 .ToArray(),因为 string.Join 方法已重载以接受 IEnumerable

上面的代码适用于真正的 ASCII,因为 UTF16 的前 128 个代码点(C# 的 string 类型中使用的编码)具有与 ASCII 相同的数值,因此转换 C# char 值转换为 int 就可以了。然而,通常被描述为“ASCII”的实际上是一些 ANSI 代码页(在美国,通常是代码页 1252,“西欧(Windows”),它有 256 个代码点,第二个 128 的值与它不同。 <

如果您正在处理该问题或任何其他代码页,并且您的文本为 C# string,则可以应用与上面相同的技术,但使用 code>Encoding 类,用于在转换为十六进制之前将 C# string 对象转换为 byte[]

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);

Test this

string input = "12345";
string hex = string.Join(string.Empty,
    input.Select(c => ((int)c).ToString("X")).ToArray());

Console.WriteLine(hex);

Note: in C# 4, the call to .ToArray() is not necessary because the string.Join method has been overloaded to accept IEnumerable<T>.

The above will work for real ASCII, because the first 128 code points of UTF16 (the encoding used in C#'s string type) have the same numeric values as for ASCII, and so casting the C# char value to int is fine. However, often what is described as "ASCII" is really some ANSI code page (in the US, usually code page 1252, "Western European (Windows"), which has 256 code points, the second 128 not having the same values as that used in UTF16.

If you are dealing with that, or any other code page for that matter, and you have the text as a C# string, you can apply the same technique as above, except using the Encoding class to convert the C# string object to a byte[] before converting to hexadecimal:

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);
救星 2024-09-06 07:59:25

将字符转换为 ASCII

int c = (int)'a';

Convert Char to ASCII

int c = (int)'a';
空名 2024-09-06 07:59:25

与 Anthony Pegram 的解决方案类似,但更类似于 LINQ,并且更短,但由于聚合方法中的多个字符串分配而速度较慢。

string hex = input.Select(c => ((int)c).ToString("X")).Aggregate((a, s) => a + s);

Similair to Anthony Pegram's solution but more LINQ'ish and a tad shorter, but slower du to multiple string allocations in the aggregate method.

string hex = input.Select(c => ((int)c).ToString("X")).Aggregate((a, s) => a + s);
我最亲爱的 2024-09-06 07:59:25

尝试使用此方法,

public static string AsciiToHexadecimal(List<int> asciiList)
{
        return asciiList.Aggregate("", (current, i) => current + $"{i:X}");
}

该方法将帮助您将 ASCII 数字数组转换为十六进制。

Try using this method

public static string AsciiToHexadecimal(List<int> asciiList)
{
        return asciiList.Aggregate("", (current, i) => current + 
quot;{i:X}");
}

this's a method will help you to convert an array of ASCII numbers to Hexadecimal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文