用于解码无符号短值的函数

Function for decoding unsigned short value

本文关键字:函数 解码 无符号 用于      更新时间:2023-10-16

我对某些任务有小问题。

我们对此主题进行了调查。单个调查的结果(从一个受访者获得)提供了以下信息,这些信息将被编码为无符号短变量(可以假设它是 2 个字节 - 16 位)

  1. 性别 - 1 位 - 2 种可能性
  2. 婚姻状况 - 2 位 - 4 种可能性
  3. 年龄 - 2 位 - 4 种可能性
  4. 教育 - 2 位 - 4 种可能性
  5. 城市 - 2 位 - 4 种可能性
  6. 区域 - 4 位 - 16 种可能性
  7. 答案 - 3 位 - 8 种可能性

     unsigned short coding(int sex, int marital_status, int age, int edu, int city, int region, int reply){
        unsigned short result = 0;
        result = result + sex;
        result = result + ( marital_status << 1 );
        result = result + ( age << 3);
        result = result + ( edu << 5 );
        result = result + ( city << 6 );
        result = result + ( region << 11 );
        result = result + ( reply << 13 );
        return result;
    }
    

在这里,它对结果进行编码(希望它是正确的),但我不知道如何准备将显示信息的函数,我已经在无符号的短 x 中编码了这些信息。

首先,我必须对其进行编码:

     unsigned short x = coding(0, 3, 2, 3, 0, 12, 6);

然后我需要准备另一个函数,它将无符号短 X 中的信息解码为这种形式:

    info(x);

    RESULT
    sex:                 0
    martial status:      3
    age:                 2
    education:           3
    city:                0
    region:              12
    reply:               6

我将感谢您的帮助,因为我甚至不知道如何开始以及寻找什么。

我的问题是是否有人可以检查无符号的短编码功能并帮助编写无效信息(无符号短 x)。

您可以使用位字段

struct survey_data
{
    unsigned short sex            : 1;
    unsigned short marital_status : 2;
    unsigned short age            : 2;
    unsigned short education      : 2;
    unsigned short city           : 2;
    unsigned short region         : 4;
    unsigned short answer         : 3;
};

如果需要在短之间转换它,可以像这样定义一个联合

union survey
{
    struct survey_data detail;
    unsigned short s;
};

使用这些类型

struct survey_data sd;
sd.sex = 0;
sd.marital_status = 2;
...
unsigned short s = 0xCAFE;
union servey x;
x.s = s;
printf("Sex: %u, Age: %u", x.detail.sex, x.detail.age);

请记住,位字段的布局是实现定义的;不同的编译器可能以不同的顺序解释它们,例如在MSVC中,它是LSB到MSB;有关详细信息,请参阅编译器手册和C/C ++标准。

解决方案很简单,主要是文本工作。传输您的数据描述

sex - 1 bit - 2 possibilities
marital status - 2 bits - 4 possibilities
Age - 2 bits - 4 possibilities
Education - 2 bits - 4 possibilities
City - 2 bits - 4 possibilities
region - 4 bits - 16 possibilities
answer - 3 bits - 8 possibilities

进入此 C/C++ 结构:

struct Data {
    unsigned sex:        1; //  2 possibilities
    unsigned marital:    2; //  4 possibilities
    unsigned Age:        2; //  4 possibilities
    unsigned Education:  2; //  4 possibilities
    unsigned City:       2; //  4 possibilities
    unsigned region:     4; // 16 possibilities
    unsigned answer:     3; //  8 possibilities
};

这是位集的标准用例,甚至是传统的 C,但也可用于每个符合标准的C++实现。

让我们为存储store_t命名 16 位编码数据类型(从使用的几个定义中,我们使用 C 标准标头 stdint.h):

#include <stdint.h>
typedef uint16_t store_t;

示例Data结构可用于编码

/// create a compact storage type value from data
store_t encodeData(const Data& data) {
    return *reinterpret_cast<const store_t*>(&data);
}

解码数据集:

/// retrieve data from a compact storage type
const Data decodeData(const store_t code) {
    return *reinterpret_cast<const Data*>(&code);
}

您可以像普通结构一样Data访问位集结构:

Data data;
data.sex = 1;
data.marital = 0;