将文件中的十六进制添加到列表<byte>
Add hexadecimal from file to List<byte>
我正在尝试从包含字符串 (Hexadecimal) 的文件中生成 List<byte>
。
定义是:
List<byte> myArray = new List<byte>();
如果我想直接添加我的信息,我会使用这样的东西:
myArray.Add(0xb8);
注意:没有任何引号或双引号。
问题是当我想从文件做同样的事情时!
现在我想知道 0xb8
的类型是什么,所以我使用以下代码:
0xc3.GetType().ToString()
结果是:System.Int32 !!!!
但是当我从文件中读取字符串并使用这样的代码时,出现以下错误。
代码:
Line = "0xb8";
myArray.Add(Convert.ToInt32(Line));
错误:
Argument 1: cannot convert from 'int' to 'byte'
很清楚。因为 myArray
的唯一重载只得到一个 byte
作为参数。
让事情对我来说如此复杂的原因是为什么当我在 myArray.Add(0xb8);
中向 myArray
添加 Int32
时它没有给我任何错误。
我觉得应该是byte的一种形式!也许!
为什么它不给出任何错误以及如何完成这种情况(我的意思是将字符串中的字节添加到 myArray )?
How can accomplish this scenario (I mean add byte from string to myArray ) ?
List<byte> myArray = new List<byte>();
string Line = "0xb8";
myArray.Add(Convert.ToByte(Line, 16));
why it doesn't give me any error when I add a Int32 to myArray in myArray.Add(0xb8);
编译器可以看到 0xb8
在 Byte
值范围内
myArray.Add(0xb8); // ok
myArray.Add(0xff); // ok
myArray.Add(0x100); // cannot convert from int to byte
0xb8
是整数文字(如 1234
)。 "0xb8"
是一个字符串文字(如 "x"
)。 C# 中没有十六进制文字这样的东西。它是十六进制格式的 int 文字。这样就解释了您在文字方面遇到的麻烦。
并非所有的字符串解析函数都支持十六进制输入。参见 this for how to parse a hex string。
当一个解析函数给你一个 int
而你需要把它当作一个 byte
时,你可以转换:(byte)myInt
。这是安全的,因为解析后的值保证适合一个字节。
在某些地方,如果您一直在使用 0xb8
等常量,编译器可以为您自动转换。由于您在实践中不会遇到这种情况(您正在解析文件),因此这种情况与最终代码无关。但这解释了为什么它有效。
这个有效:
var list = new List<byte>();
byte byteValue = 0xb8;
list.Add(byteValue);
这不是 ("cannot convert from 'int' to 'byte'"):
var list = new List<byte>();
int byteValue = 0xb8;
list.Add(byteValue);
然而在这两种情况下 0xb8
都被识别为 System.Int32
。那么,为什么编译器允许在第一个示例中将 int
隐式转换为 byte
?
请参阅 C# 规范 6.1.9 隐式常量表达式转换:
A constant-expression (§7.19 [including literals]) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.
这是因为所有整数文字都是 (u)int
或 (u)long
,如 2.4.4.2 整数文字中所述。
给定 0 <= 0xb8 <= 255
,编译器允许隐式转换。
在您的情况下,由于 的原因,您需要进行显式转换:
myArray.Add((byte)Convert.ToInt32(Line, 16));
您只需将值转换为 byte
:
即可消除该编译器错误
myArray.Add((byte)Convert.ToInt32(Line));
但是,当您 运行 该代码时,您会得到一个不同的错误:
Unhandled Exception: System.FormatException: Input string was not in a
correct format.
那是因为 Convert.ToInt32
方法无法直接解析十六进制数。
字符串转数字时可以指定基数:
myArray.Add((byte)Convert.ToInt32(Line, 16));
我正在尝试从包含字符串 (Hexadecimal) 的文件中生成 List<byte>
。
定义是:
List<byte> myArray = new List<byte>();
如果我想直接添加我的信息,我会使用这样的东西:
myArray.Add(0xb8);
注意:没有任何引号或双引号。
问题是当我想从文件做同样的事情时!
现在我想知道 0xb8
的类型是什么,所以我使用以下代码:
0xc3.GetType().ToString()
结果是:System.Int32 !!!!
但是当我从文件中读取字符串并使用这样的代码时,出现以下错误。
代码:
Line = "0xb8";
myArray.Add(Convert.ToInt32(Line));
错误:
Argument 1: cannot convert from 'int' to 'byte'
很清楚。因为 myArray
的唯一重载只得到一个 byte
作为参数。
让事情对我来说如此复杂的原因是为什么当我在 myArray.Add(0xb8);
中向 myArray
添加 Int32
时它没有给我任何错误。
我觉得应该是byte的一种形式!也许!
为什么它不给出任何错误以及如何完成这种情况(我的意思是将字符串中的字节添加到 myArray )?
How can accomplish this scenario (I mean add byte from string to myArray ) ?
List<byte> myArray = new List<byte>();
string Line = "0xb8";
myArray.Add(Convert.ToByte(Line, 16));
why it doesn't give me any error when I add a Int32 to myArray in myArray.Add(0xb8);
编译器可以看到 0xb8
在 Byte
值范围内
myArray.Add(0xb8); // ok
myArray.Add(0xff); // ok
myArray.Add(0x100); // cannot convert from int to byte
0xb8
是整数文字(如 1234
)。 "0xb8"
是一个字符串文字(如 "x"
)。 C# 中没有十六进制文字这样的东西。它是十六进制格式的 int 文字。这样就解释了您在文字方面遇到的麻烦。
并非所有的字符串解析函数都支持十六进制输入。参见 this for how to parse a hex string。
当一个解析函数给你一个 int
而你需要把它当作一个 byte
时,你可以转换:(byte)myInt
。这是安全的,因为解析后的值保证适合一个字节。
在某些地方,如果您一直在使用 0xb8
等常量,编译器可以为您自动转换。由于您在实践中不会遇到这种情况(您正在解析文件),因此这种情况与最终代码无关。但这解释了为什么它有效。
这个有效:
var list = new List<byte>();
byte byteValue = 0xb8;
list.Add(byteValue);
这不是 ("cannot convert from 'int' to 'byte'"):
var list = new List<byte>();
int byteValue = 0xb8;
list.Add(byteValue);
然而在这两种情况下 0xb8
都被识别为 System.Int32
。那么,为什么编译器允许在第一个示例中将 int
隐式转换为 byte
?
请参阅 C# 规范 6.1.9 隐式常量表达式转换:
A constant-expression (§7.19 [including literals]) of type int can be converted to type sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant-expression is within the range of the destination type.
这是因为所有整数文字都是 (u)int
或 (u)long
,如 2.4.4.2 整数文字中所述。
给定 0 <= 0xb8 <= 255
,编译器允许隐式转换。
在您的情况下,由于
myArray.Add((byte)Convert.ToInt32(Line, 16));
您只需将值转换为 byte
:
myArray.Add((byte)Convert.ToInt32(Line));
但是,当您 运行 该代码时,您会得到一个不同的错误:
Unhandled Exception: System.FormatException: Input string was not in a correct format.
那是因为 Convert.ToInt32
方法无法直接解析十六进制数。
字符串转数字时可以指定基数:
myArray.Add((byte)Convert.ToInt32(Line, 16));