C#编码、解码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Exclaiming/article/details/84887695

编码四元数:

//使用前先申请空间
private  byte[] currentFrameLeft = new byte[4 * sizeof(double)];//里面放置4个double类型的数

//编码四元数,从0开始每次间隔一个double的长度存一个值
public byte[] EncodeLeftToByteArray()
    {
        int index = 0;
       
        byte[] qua_x = BitConverter.GetBytes((double)Direction.x);
        qua_x.CopyTo(currentFrameLeft, index);
        index += sizeof(double);
        byte[] qua_y = BitConverter.GetBytes((double)Direction.y);
        qua_y.CopyTo(currentFrameLeft, index);
        index += sizeof(double);
        byte[] qua_z = BitConverter.GetBytes((double)Direction.z);
        qua_z.CopyTo(currentFrameLeft, index);
        index += sizeof(double);
        byte[] qua_w = BitConverter.GetBytes((double)Direction.w);
        qua_w.CopyTo(currentFrameLeft, index);
        return currentFrameLeft;
}




//解码四元数,每次间隔一个double的长度取一个值,注意前后对应,前面放什么类型的数据后面需要对应解码什么数据
public void LoadRemoteLeftHand(byte[] frame)
    {
        int index = 0;

        Direction.x= (float)BitConverter.ToDouble(frame, index);
        index += sizeof(double);
        Direction.y = (float)BitConverter.ToDouble(frame, index);
        index += sizeof(double);
        Direction.z = (float)BitConverter.ToDouble(frame, index);
        index += sizeof(double);
        Direction.w = (float)BitConverter.ToDouble(frame, index);
}

猜你喜欢

转载自blog.csdn.net/Exclaiming/article/details/84887695
今日推荐