问题描述
我如何以十六进制表示法构建转义序列字符串。
How do I build an escape sequence string in hexadecimal notation.
示例:
Example:
string s = "\x1A"; // this will create the hex-value 1A or dec-value 26
我希望能够使用十六进制值在00到FF之间构建字符串(在本例中为1B)
I want to be able to build strings with hex-values between 00 to FF like this (in this example 1B)
string s = "\x" + "1B"; // Unrecognized escape sequence
也许还有另一种制作十六进制字符串的方法...
Maybe there's another way of making hexadecimal strings...
推荐答案
您不会在字符串中存储十六进制值。
You don't store hexadecimal values in strings.
你可以,但它只是一个字符串,必须转换为整数或字节才能真正读取它的值。
You can, but it would just be that, a string, and would have to be cast to an integer or a byte to actually read its value.
您可以将一个十六进制值作为一个文字分配给一个int或一个字节:
You can assign a hexadecimal value as a literal to an int or a byte though:
Byte value = 0x0FF;
int value = 0x1B;
所以,它很容易将十六进制字面值传递到字符串中:
So, its easily possible to pass an hexadecimal literal into your string:
string foo = String.Format("{0} hex test", 0x0BB);
哪个会创建这个字符串126 hex test。
Which would create this string "126 hex test".
但我不认为这就是你想要的?
But I don't think that's what you wanted?
这篇关于C#构建十六进制表示法字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!