- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这种方法可以将长字节数组转换为小端字节数组
public static byte[] UnsignedIntegerToLEByteArray(ulong value)
{
// Value in bytes... in your system's endianness (let's say: little endian)
byte[] bytes = BitConverter.GetBytes(value);
// If it was big endian, reverse it
if (!BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return bytes;
}
我的目标是也将它用于短数据类型,如 int、short 等。请参见此处:
byte a = 0xAA;
ushort b = 0xEEAA;
uint c = 0xAABBCCDD;
ulong d = 0xAABBCCDDAAAAAABB;
// If you passed short below, you are only interested
// in first two bytes of the array
byte []tmp = DppUtilities.UnsignedIntegerToLEByteArray(b);
如果我的机器是小端字节序,这会起作用。如果它在大端机器上运行它也会工作吗?我想是的,但我想验证一下。
最佳答案
你可以在 IntPtr
和 Marshal
的帮助下玩一个trick 来转换任何struct (包括byte
, ushort
和ulong
):
// Disclaimer: the structure will be reversed as a whole, not field by field
public static byte[] ToLEByteArray<T>(T value) where T: struct {
int size = Marshal.SizeOf(typeof(T));
byte[] bytes = new byte[size];
IntPtr p = Marshal.AllocHGlobal(size);
try {
Marshal.StructureToPtr(value, p, true);
Marshal.Copy(p, bytes, 0, size);
}
finally {
Marshal.FreeHGlobal(p);
}
// If it was big endian, reverse it
if (!BitConverter.IsLittleEndian)
Array.Reverse(bytes);
return bytes;
}
....
Byte b = 123;
ushort s = 123;
ulong l = 123;
Byte[] result_byte = ToLEByteArray(b);
Byte[] result_ushort = ToLEByteArray(s);
Byte[] result_ulong = ToLEByteArray(l);
....
int i = 123456;
Byte[] result_int = ToLEByteArray(i);
编辑:问题中的实现有什么问题? (来自评论)。 或者,重申一下这个问题,IntPtr
、Marshal
的作用是什么?
问题实现的主要问题是初始转换到ulong
:
// all the arguments will be converted to ulong
public static byte[] UnsignedIntegerToLEByteArray(ulong value)
为了说明问题,想象一下,我们有两个值
Byte x = 0x12; // 18
ulong u = 0x0000000000000012; // 18
预期的输出是
new byte[] {0x12}; // for a single byte
new byte[] {0x12, 0, 0, 0, 0, 0, 0, 0}; // for 8 bytes, i.e. ulong
然而,实际输出将是
new byte[] {0x12, 0, 0, 0, 0, 0, 0, 0};
对于 byte
和 ulong
。如果您想要将数值(byte
、short
、ulong
等)写到一个二进制文件,将它们传递给二进制流等:
using (Stream stm = ...) {
...
Byte[] buffer = UnsignedIntegerToLEByteArray(...);
stm.Write(buffer, offset, buffer.Length); // <- the (possibly!) erroneous write
...
}
关于c# - 以小端方式将整数转换为字节数组(通用函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33097763/
我正在尝试在 Raspberry Pi(使用 C 编程)和 Arduino(使用 Arduino IDE)之间发送和接收数据。 根据我在互联网上可以找到的内容,它指出两者都是小端格式。 我在 teun
考虑这段代码: int a=0x10000001; char b; b=(char)a; printf("%#x \n",b); 在我的 PC 上它打印 0x01,我并不感到惊讶。它在 BIG END
我需要你的帮助。我想使用以下代码,因为我正在开发一个音频工具(仅适用于 .wav 文件),其主要功能是显示信号波形。大端和小端是我无法控制的元素。我认为以下代码解决问题的方式是正确的:- 如果音频文件
我有一个十六进制字符串,其内容为18000000,该字符串采用主机字节顺序(小端),我需要将其转换为网络字节顺序(大端)。生成的十六进制字符串将为 00000018。 总而言之,我需要转换180000
我有一个十六进制字符串,其内容为18000000,该字符串采用主机字节顺序(小端),我需要将其转换为网络字节顺序(大端)。生成的十六进制字符串将为 00000018。 总而言之,我需要转换180000
我有一个整数列表,比如说 L,它包含一个数字的二进制表示。列表L中的每个整数可以是0或1。“最低有效位”在左边(不是在右边)。 示例:1000001111 代表(十进制)数字 961,或 011101
我有来自实验的Base64 编码数据。所以我想逐步做的是: 从 base64 编码中检索字节(解码) 将字节转换为小端 从 (zlib) 中解压字节 字节数组转 float 组 示例: Dn3LQ3n
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
解码字节时,binary.Read()要求您指定该操作的预期字节顺序。 binary.Read() 还允许您传入结构,但据我所知,它使用相同的字节顺序将字节流解码到结构中的每个字段中。 当编码整数的字
我将 24 位单声道音频 .wav 文件读入 > 7) * 255 data = a.view('
我正在解码一个二进制文件,它有用四个字节表示的十进制数字,小端。例如,94 53 F0 40代表 7.510202。不幸的是,Python 给了我 7.51020240784。 当我尝试使用 unpa
这种传输语法中的数据是如何组织的?来自标准的描述: This Transfer Syntax applies to the encoding of the entire DICOM Data Set.
我是一名优秀的程序员,十分优秀!