需要将ascii值转换为hex值
我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测试此
注意:在 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[]
:Test this
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 toint
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 theEncoding
class to convert the C#string
object to abyte[]
before converting to hexadecimal:将字符转换为 ASCII
Convert Char to ASCII
与 Anthony Pegram 的解决方案类似,但更类似于 LINQ,并且更短,但由于聚合方法中的多个字符串分配而速度较慢。
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.
尝试使用此方法,
该方法将帮助您将 ASCII 数字数组转换为十六进制。
Try using this method
this's a method will help you to convert an array of ASCII numbers to Hexadecimal.