简体   繁体   English

将 Int 数组转换为 C# 中的 Little-Endian 字节数组

[英]Convert array of Ints into array of Little-Endian Bytes in C#

I need to convert a large array of ints into an array of their little endian byte representations.我需要将大量的整数转换成它们的小字节序字节表示形式的数组。 Is there a function that converts the array or is the only way to loop through and "manually" convert each one.是否有一个 function 可以转换数组,或者是循环遍历并“手动”转换每个数组的唯一方法。

One thing you could do is use the EndianBitConverter project from Nuget. It does what it says on the tin and provides a way of converting to either big-endian or little-endian.您可以做的一件事是使用来自 Nuget 的EndianBitConverter项目。它按照罐头上的说明进行操作,并提供了一种转换为大端或小端的方法。

Once you have got it installed, you can write code like this:安装完成后,您可以编写如下代码:

var intList = new List<int> { 91233, 67278, 22345, 45454, 23449 };
            
foreach ( var n in intList)
{
    var result1 = EndianBitConverter.LittleEndian.GetBytes(n);
    var result2 = EndianBitConverter.BigEndian.GetBytes(n);
}

If you want to do it as a 1 liner, then perhaps something like this:如果你想把它作为 1 班轮来做,那么也许是这样的:

var intList = new List<int> { 91233, 67278, 22345, 45454, 23449 };
var result = intList.Select(x => EndianBitConverter.LittleEndian.GetBytes(x)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM