欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实现16进制和字符串之间转换的代码

程序员文章站 2023-11-14 14:01:16
十六进制字符串与数值类型之间转换(c# 编程指南) 以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值。 获取与十六进制字符串中的每个值对应的字符。 将十六进...

十六进制字符串与数值类型之间转换(c# 编程指南)
以下示例演示如何执行下列任务:
获取字符串中每个字符的十六进制值。
获取与十六进制字符串中的每个值对应的字符。
将十六进制 string 转换为整型。
将十六进制 string 转换为浮点型。
将字节数组转换为十六进制 string。
示例
此示例输出 string 中的每个字符的十六进制值。首先,它将 string 分析为字符数组,然后对每个字符调用 toint32(char) 以获取相应的数字值。最后,在 string 中将数字的格式设置为十六进制表示形式。
c#
string input = "hello world!";
char[] values = input.tochararray();
foreach (char letter in values)
{
// get the integral value of the character.
int value = convert.toint32(letter);
// convert the decimal value to a hexadecimal value in string form.
string hexoutput = string.format("{0:x}", value);
console.writeline("hexadecimal value of {0} is {1}", letter, hexoutput);
}
/* output:
hexadecimal value of h is 48
hexadecimal value of e is 65
hexadecimal value of l is 6c
hexadecimal value of l is 6c
hexadecimal value of o is 6f
hexadecimal value of is 20
hexadecimal value of w is 57
hexadecimal value of o is 6f
hexadecimal value of r is 72
hexadecimal value of l is 6c
hexadecimal value of d is 64
hexadecimal value of ! is 21
*/
此示例分析十六进制值的 string 并输出对应于每个十六进制值的字符。首先,它调用 split(array<char>[]()[]) 方法以获取每个十六进制值作为数组中的单个 string。然后调用 toint32(string, int32) 以将十六进制转换为表示为 int 的十进制值。示例中演示了用于获取对应于该字符代码的字符的两种不同方法。第一种方法是使用 convertfromutf32(int32),它将对应于整型参数的字符作为 string 返回。第二种方法是将 int 显式转换为 char。
c#
string hexvalues = "48 65 6c 6c 6f 20 57 6f 72 6c 64 21";
string[] hexvaluessplit = hexvalues.split(' ');
foreach (string hex in hexvaluessplit)
{
// convert the number expressed in base-16 to an integer.
int value = convert.toint32(hex, 16);
// get the character corresponding to the integral value.
string stringvalue = char.convertfromutf32(value);
char charvalue = (char)value;
console.writeline("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
hex, value, stringvalue, charvalue);
}
/* output:
hexadecimal value = 48, int value = 72, char value = h or h
hexadecimal value = 65, int value = 101, char value = e or e
hexadecimal value = 6c, int value = 108, char value = l or l
hexadecimal value = 6c, int value = 108, char value = l or l
hexadecimal value = 6f, int value = 111, char value = o or o
hexadecimal value = 20, int value = 32, char value = or
hexadecimal value = 57, int value = 87, char value = w or w
hexadecimal value = 6f, int value = 111, char value = o or o
hexadecimal value = 72, int value = 114, char value = r or r
hexadecimal value = 6c, int value = 108, char value = l or l
hexadecimal value = 64, int value = 100, char value = d or d
hexadecimal value = 21, int value = 33, char value = ! or !
*/
此示例演示了将十六进制 string 转换为整数的另一种方法,即调用 parse(string, numberstyles) 方法。
c#
string hexstring = "8e2";
int num = int32.parse(hexstring, system.globalization.numberstyles.hexnumber);
console.writeline(num);
//output: 2274
下面的示例演示如何使用 system..::.bitconverter 类和 int32..::.parse 方法将十六进制 string 转换为浮点型。
c#
string hexstring = "43480170";
uint num = uint.parse(hexstring, system.globalization.numberstyles.allowhexspecifier);
byte[] floatvals = bitconverter.getbytes(num);
float f = bitconverter.tosingle(floatvals, 0);
console.writeline("float convert = {0}", f);
// output: 200.0056
下面的示例演示如何使用 system..::.bitconverter 类将字节数组转换为十六进制字符串。
c#
byte[] vals = { 0x01, 0xaa, 0xb1, 0xdc, 0x10, 0xdd };
string str = bitconverter.tostring(vals);
console.writeline(str);
str = bitconverter.tostring(vals).replace("-", "");
console.writeline(str);
/*output:
01-aa-b1-dc-10-dd
01aab1dc10dd
*/
只是msdn上的盗版!

复制代码 代码如下:

public string strtohex(string mstr) //返回处理后的十六进制字符串
{
return bitconverter.tostring(
asciiencoding.default.getbytes(mstr)).replace("-", " ");
} /* strtohex */
public string hextostr(string mhex) // 返回十六进制代表的字符串
{
mhex = mhex.replace(" ", "");
if (mhex.length <= 0) return "";
byte[] vbytes = new byte[mhex.length / 2];
for (int i = 0; i < mhex.length; i += 2)
if (!byte.tryparse(mhex.substring(i, 2), numberstyles.hexnumber, null, out vbytes[i / 2]))
vbytes[i / 2] = 0;
return asciiencoding.default.getstring(vbytes);
} /* hextostr */