需要将字符串/字符转换为ASCII值 [英] Need to convert string/char to ascii values

查看:165
本文介绍了需要将字符串/字符转换为ASCII值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  • 字符1 = 31 2 = 32 3 = 33 4 = 34 5 = 35 A = 41 a = 61等
  • char 1 = 31 2 = 32 3 = 33 4 = 34 5 = 35 A = 41 a = 61 etc

因此,字符串str ="12345"; 需要获取转换后的str ="3132333435"

Therefore string str = "12345"; Need to get the converted str = "3132333435"

推荐答案

我认为这就是您所需要的:

I think this is all you'll need:

string finalValue;
byte[] ascii = Encoding.ASCII.GetBytes(yourString);
foreach (Byte b in ascii) 
{
  finalValue += b.ToString("X");
}

有关MSDN的更多信息: http://msdn .microsoft.com/en-us/library/system.text.encoding.ascii.aspx

More info on MSDN: http://msdn.microsoft.com/en-us/library/system.text.encoding.ascii.aspx

十六进制:

string finalValue;
int value;
foreach (char c in myString)
{
  value = Convert.ToInt32(c);
  finalValue += value.ToString("X"); 
  // or finalValue = String.Format("{0}{1:X}", finalValue, value);
}
// use finalValue

这篇关于需要将字符串/字符转换为ASCII值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆