[英]Convert a byte to a number C#
How can I convert a byte to a number in C#? 如何在C#中将字节转换为数字? For example, 00000001 to 1, 00000011 to 3, 00001011 to 11. i have a byte array with numbers encoded as binary bytes, but I need to get those numbers and append them to a string.
例如,00000001到1,00000011到3,00001011到11。我有一个字节数组,其数字编码为二进制字节,但是我需要获取这些数字并将其附加到字符串中。
You can do this. 你可以这样做。
// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
int i = BitConverter.ToInt32(bytes, 0);
where bytes is your bytes[]. 其中bytes是您的bytes []。 You would want to take a look here
您想在这里看看
In C# byte
is already an unsigned number ranging from 0
to 255
. 在C#中,
byte
已经是一个无符号数字,范围是0
到255
。 You can freely assign them to integers, or convert to other numeric types. 您可以将它们自由分配给整数,也可以转换为其他数字类型。
Bytes are numbers. 字节是数字。
If you want to get the numeric value of a single byte
, just call ToString()
. 如果要获取单个
byte
的数值,只需调用ToString()
。
If you have an array of bytes that are part of a little-endian single number, you can use the BitConverter
class to convert them to a 16, 32, or 64 bit signed or unsigned integer. 如果您的字节数组是小尾数单个数字的一部分,则可以使用
BitConverter
类将它们转换为16位,32位或64位有符号或无符号整数。
you can use built-in Convert 您可以使用内置的转换
foreach (byte b in array) {
long dec = Convert.ToInt64(b,2);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.