在 C# 中将单个十六进制字符转换为其字节值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1214980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert a single hex character to its byte value in C#
提问by nos
This will convert 1 hex character to its integer value, but needs to construct a (sub) string.
这会将 1 个十六进制字符转换为其整数值,但需要构造一个(子)字符串。
Convert.ToInt32(serializedString.Substring(0,1), 16);
Does .NET have a built-in way to convert a single hex character to its byte (or int, doesn't matter) value that doesn't involve creating a new string?
.NET 是否具有将单个十六进制字符转换为其字节(或 int,无所谓)值的内置方法,而不涉及创建新字符串?
采纳答案by Ben M
int value = "0123456789ABCDEF".IndexOf(char.ToUpper(sourceString[index]));
Or even faster (subtraction vs. array search), but no checking for bad input:
或者甚至更快(减法与数组搜索),但不检查错误输入:
int HexToInt(char hexChar)
{
hexChar = char.ToUpper(hexChar); // may not be necessary
return (int)hexChar < (int)'A' ?
((int)hexChar - (int)'0') :
10 + ((int)hexChar - (int)'A');
}
回答by JaredPar
If you know the hex value is only a byte then just convert to an Int32 and then cast
如果您知道十六进制值只是一个字节,那么只需转换为 Int32 然后转换
var b = (byte)(Convert.ToInt32(serializedString, 16));
回答by Paul van Brenk
Encoding.UTF8.GetBytes( serializedString.ToCharArray(), 0, 1)
Cheaper might be:
更便宜的可能是:
Encoding.UTF8.GetBytes( new char[]{ serializedString[0] }, 0, 1)
This will only add the interesting char to the char[] and not the entire string.
这只会将有趣的字符添加到 char[] 而不是整个字符串。
回答by almog.ori
Correct me if im wrong but can you simply use
如果我错了,请纠正我,但您可以简单地使用
Convert.ToByte(stringValue, 16);
as long as the stringValue represents a hex number? Isnt that the point of the base paramter?
只要 stringValue 代表一个十六进制数?这不是基本参数的重点吗?
Strings are immutable, I dont think there is a way to get the substring byte value of the char at index 0 without creating a new string
字符串是不可变的,我认为没有办法在不创建新字符串的情况下获取索引 0 处的 char 的子字符串字节值
回答by Ben Lesh
Sure you can get the hex value without ever needing to create another string. I'm not sure what it'll really gain you, performance wise, but since you asked, here's what you've requested.
当然,您无需创建另一个字符串即可获得十六进制值。我不确定它会给你带来什么,在性能方面,但既然你问了,这就是你所要求的。
public int FromHex(ref string hexcode, int index)
{
char c = hexcode[index];
switch (c)
{
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'A':
case 'a':
return 0xa;
case 'B':
case 'b':
return 0xb;
case 'C':
case 'c':
return 0xc;
case 'D':
case 'd':
return 0xd;
case 'E':
case 'e':
return 0xe;
case 'F':
case 'f':
return 0xf;
case '0':
default:
return 0;
}
}
}
回答by polfosol ?_?
I am adding another answer just because no one mentioned this one. You can use the built-in Uri.FromHex
method for converting a single character:
我添加另一个答案只是因为没有人提到这个。您可以使用内置Uri.FromHex
方法转换单个字符:
var b = (byte) System.Uri.FromHex('a'); // b = 10