对位字段结构转换字节序 [英] Converting Endianess on a bit field structure

查看:139
本文介绍了对位字段结构转换字节序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个位字段结构与小端转换为大endia架构。
什么是做到这一点的最好方式,因为会在字节边界的问题,如果我只是交换结构元素。

防爆结构是:

 结构{
    unsigned int类型B1:1;
    unsigned int类型B2:8;
    unsigned int类型B3:7;
    unsigned int类型B4:8;
    unsigned int类型B5:7;
    unsigned int类型B6:1;
};


解决方案

要得到这个正在进行的我终于得到了一个解决方案(有些东西从epatel的解决方案上面的推导)。这是,如果我从86转换为Solaris SPARC。

我们需要先换入构成及读取,然后以相反的顺序的元素。
看结构神韵:如何后基本上我看到这两个字节序字节顺序和位序变化。这里是一个伪code。

 结构原稿
{
    unsigned int类型B1:1;
    unsigned int类型B2:8;
    unsigned int类型B3:7;
    unsigned int类型B4:8;
    unsigned int类型B5:7;
    unsigned int类型B6:1;
};结构温度
{
    unsigned int类型B6:1;
    unsigned int类型B5:7;
    unsigned int类型B4:8;
    unsigned int类型B3:7;
    unsigned int类型B2:8;
    unsigned int类型B1:1;
}温度;
FUNC(结构原稿* toconvert)
{
    结构温度temp_val;
    //交换字节
    swap32byte((U32 *)toconvert);
    //现在读以相反的顺序结构 - 字节交换
    (U32 *)及temp_val =(U32 *)toconvert;
    //写回一部开拓创新结构
    toconvert-> B6 = temp_val.b6;
    toconvert-> B5 = temp_val.b5;
    toconvert-> B4 = temp_val.b4;
    toconvert-> B3 = temp_val.b3;
    toconvert-> B2 = temp_val.b2;
    toconvert-> B1 = temp_val.b1;

}

在一些实验,我发现,这种方法才有效,如果要素完全填满的结构,即没有未使用位。

I need to convert a bit-field structure from little-endian to big-endia architecture. What is the best way to do that, as there will be issues in byte boundaries, if I simply swap the structure elements.

Ex Structure is:

struct {
    unsigned int    b1:1;
    unsigned int    b2:8;
    unsigned int    b3:7;
    unsigned int    b4:8;
    unsigned int    b5:7;
    unsigned int    b6:1;
};

解决方案

To get this going I finally got a solution (some what derived from epatel's solution above). This is if I convert from x86 to Solaris SPARC.

We need to first swap the incoming sturcture and then read the elements in reverse order. Basically after looking at how the structures are alligned I saw that the endianess changed both in byte ordering and bit ordering. Here is a pseudo code.

struct orig
{    
    unsigned int    b1:1;
    unsigned int    b2:8;
    unsigned int    b3:7;
    unsigned int    b4:8;
    unsigned int    b5:7;
    unsigned int    b6:1;
};

struct temp
{    
    unsigned int    b6:1;
    unsigned int    b5:7;
    unsigned int    b4:8;
    unsigned int    b3:7;
    unsigned int    b2:8;
    unsigned int    b1:1;
}temp;


func (struct orig *toconvert)
{
    struct temp temp_val;
    //Swap the bytes
    swap32byte((u32*)toconvert);
    //Now read the structure in reverse order - bytes have been swapped
    (u32*)&temp_val = (u32 *)toconvert;
    //Write it back to orignal structure
    toconvert->b6=temp_val.b6;
    toconvert->b5=temp_val.b5;
    toconvert->b4=temp_val.b4;
    toconvert->b3=temp_val.b3;
    toconvert->b2=temp_val.b2;
    toconvert->b1=temp_val.b1;

}

After some experimenting I found that this approach is only valid if the elements completely fill the structure, i.e. there are no unused bits.

这篇关于对位字段结构转换字节序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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