常用的应用层整型编解码函数

本文详细解析了用于处理不同长度整数编码与解码的函数,包括8位、16位、24位和32位整数的转换过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int encode_unsigned16(
    uint8_t * apdu,
    uint16_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
    apdu[1] = (uint8_t) (value & 0x00ff);

    return 2;
}

int decode_unsigned16(
    uint8_t * apdu,
    uint16_t * value)
{
    if (value) {
        *value = (uint16_t) ((((uint16_t) apdu[0]) << 8) & 0xff00);
        *value |= ((uint16_t) (((uint16_t) apdu[1]) & 0x00ff));
    }

    return 2;
}

int encode_unsigned24(
    uint8_t * apdu,
    uint32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
    apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
    apdu[2] = (uint8_t) (value & 0x0000ff);

    return 3;
}

int decode_unsigned24(
    uint8_t * apdu,
    uint32_t * value)
{
    if (value) {
        *value = ((uint32_t) ((((uint32_t) apdu[0]) << 16) & 0x00ff0000));
        *value |= (uint32_t) ((((uint32_t) apdu[1]) << 8) & 0x0000ff00);
        *value |= ((uint32_t) (((uint32_t) apdu[2]) & 0x000000ff));
    }

    return 3;
}

int encode_unsigned32(
    uint8_t * apdu,
    uint32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
    apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
    apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
    apdu[3] = (uint8_t) (value & 0x000000ff);

    return 4;
}

int decode_unsigned32(
    uint8_t * apdu,
    uint32_t * value)
{
    if (value) {
        *value = ((uint32_t) ((((uint32_t) apdu[0]) << 24) & 0xff000000));
        *value |= ((uint32_t) ((((uint32_t) apdu[1]) << 16) & 0x00ff0000));
        *value |= ((uint32_t) ((((uint32_t) apdu[2]) << 8) & 0x0000ff00));
        *value |= ((uint32_t) (((uint32_t) apdu[3]) & 0x000000ff));
    }

    return 4;
}

#if BACNET_USE_SIGNED
int encode_signed8(
    uint8_t * apdu,
    int8_t value)
{
    apdu[0] = (uint8_t) value;

    return 1;
}

int decode_signed8(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFFFFFF00;
        else
            *value = 0;
        *value |= ((int32_t) (((int32_t) apdu[0]) & 0x000000ff));
    }

    return 1;
}

int encode_signed16(
    uint8_t * apdu,
    int16_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff00) >> 8);
    apdu[1] = (uint8_t) (value & 0x00ff);

    return 2;
}

int decode_signed16(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFFFF0000;
        else
            *value = 0;
        *value |= ((int32_t) ((((int32_t) apdu[0]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[1]) & 0x000000ff));
    }

    return 2;
}

int encode_signed24(
    uint8_t * apdu,
    int32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff0000) >> 16);
    apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8);
    apdu[2] = (uint8_t) (value & 0x0000ff);

    return 3;
}

int decode_signed24(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        /* negative - bit 7 is set */
        if (apdu[0] & 0x80)
            *value = 0xFF000000;
        else
            *value = 0;
        *value |= ((int32_t) ((((int32_t) apdu[0]) << 16) & 0x00ff0000));
        *value |= ((int32_t) ((((int32_t) apdu[1]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[2]) & 0x000000ff));
    }

    return 3;
}

int encode_signed32(
    uint8_t * apdu,
    int32_t value)
{
    apdu[0] = (uint8_t) ((value & 0xff000000) >> 24);
    apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16);
    apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8);
    apdu[3] = (uint8_t) (value & 0x000000ff);

    return 4;
}

int decode_signed32(
    uint8_t * apdu,
    int32_t * value)
{
    if (value) {
        *value = ((int32_t) ((((int32_t) apdu[0]) << 24) & 0xff000000));
        *value |= ((int32_t) ((((int32_t) apdu[1]) << 16) & 0x00ff0000));
        *value |= ((int32_t) ((((int32_t) apdu[2]) << 8) & 0x0000ff00));
        *value |= ((int32_t) (((int32_t) apdu[3]) & 0x000000ff));
    }

    return 4;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值