将数组列表转换为单字节[] [英] Convert array list to single byte[]

查看:59
本文介绍了将数组列表转换为单字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Codeproject。

我将结构数组列表转换为单字节数组时遇到问题。我需要这个转换,因为我必须使用ZLIB压缩列表。

这是我的列表:List< ST_Log> result = new List< ST_Log>();

结构如下所示:

Hi Codeproject.
I have a problem with converting an array list of structs to a single byte array. I need this convertion because i have to compress the list with ZLIB.
Here is my list: List<ST_Log> result = new List<ST_Log>();
And the struct look like this:

public struct ST_Log
{
    public int ProjectId;
    public int ChannelNumber;
    public int Value;
    public DateTime CreationTime;
    public bool Delete;
    public bool Invalid;
}



有没有人知道如何进行这种转换?

由Mik


Do any one have an idea of how to do this convertion?
By Mik

推荐答案

如何将每个变量转换为字节或字节[],然后将它们连接起来。你在这里拥有的每种类型都有一个固定的字节大小(日期可以转换为ToOaDate(),这是一个int)



how about just converting each variable to a byte or byte[] and then concatenating them. Each type you have here has a fixed byte size (date can be converted ToOaDate() which is an int)

...
byte[] array = objectArray.SelectMany(a=> // select many should flatten the inner array so you get a 1d array.  There are other ways to do this.
    BitConverter.GetBytes(a.ProjectId) //bitconverter for ints
    .Concat(BitConverter.GetBytes(a.ChannelNumber))
    .Concat(BitConverter.GetBytes(a.Value))
    .Concat(BitConverter.GetBytes(a.CreationTime.ToOaDate()))
    .Concat(new[]{Convert.ToByte(a.Delete)}) //Convert.ToByte for bools as they are bits
    .Concat(new[]{Convert.ToByte(a.Invalid)})).ToArray()
...





你可以转换因为你应该知道数据长度((32 + 32 + 32 + 32)/ 8 + 1 + 1)= 18



希望有帮助



You can de-convert because you should know the data length ((32+32+32+32)/8 + 1 + 1) = 18

Hope that helps


要将其转换回来:



To convert it back:

internal IEnumerable<ST_Log> DeconvertMany(byte[] converted)
{
    //check that the byte is the correct length
    if(converted.length % 18 != 0)
        throw new exception("the byte is the wrong length");
    
    for(int i = 0; i < converted.length / 18; i++){
        byte[] single = converted.take(18); // take the first 18
        converted = converted.skip(18); // remove the first 18 from the collection
        yield DeconvertOne(single);
    }
}
internal ST_Log DeconvertOne(byte[] converted){

    int projectId =  BitConverter.ToInt32(converted.take(4));
    converted = converted.skip(4);
    int channelNumber =  BitConverter.ToInt32(converted.take(4));
    converted = converted.skip(4);
    int value =  BitConverter.ToInt32(converted.take(4));
    converted = converted.skip(4);
    DateTime CreationTime = DateTime.FromOADate(BitConverter.ToInt32(converted.take(4)));
    converted = converted.skip(4);
    bool delete = Convert.ToBoolean(converted.take(1).Single();
    converted = converted.skip(1);
    bool invalid = Convert.ToBoolean(converted.take(1).Single();

    return new ST_Log
    {
        ProjectId = projectId ,
        ChannelNumber = channelNumber ,
        Value = value ,
        CreationTime = creationTime ,
        Delete = delete ,
        Invalid = invalid 
    }
}





我还没有测试过此代码所以可能需要一些调整



希望有帮助:)



I haven't tested this code so some tweaking may be required

Hope that helps :)


棒极了 - 解决方案我从来没有甚至挺身而出!

非常感谢您的帮助
Fantastic - The solution I was never even come forward to!
Many thanks for your help


这篇关于将数组列表转换为单字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆