嵌入式基础-数据的趣味学堂

嵌入式基础-数据的趣味学堂

QQ群-名称:嵌入式交流 ;群号:933192343;微信号:WangMing_GZ

1.数据不同的显示方式

C语言同样的数据可以通过不用的方式进行展示

#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main(void)
{
    int i = 0;
    int len = 0;
    unsigned char buf_str[24] = "Hello World";
    unsigned char buf_dec[24] = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100};
    unsigned char buf_hex[24] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};

    len = strlen((char *)buf_str);

    printf("dec: ");
    for(i = 0;i < len;i++)
    {
        printf("%d ",buf_str[i]);
    }
    printf("\n");


    printf("hex: ");
    for(i = 0;i < len;i++)
    {
        printf("0x%02x ",buf_str[i]);
    }
    printf("\n");

    printf("buf_dec: %s\n",buf_dec);
    printf("buf_hex: %s\n",buf_hex);

	return 0;
}

运行程序之后的打印结果

打印结果

2.数据的实践使用

在日常编程过程中,对于RAW和字符串共存的数据,不能使用 strlen 函数去判断数据的长度,要根据定义好的长度去做解析

int main(void)
{
    int len = 0;
    unsigned char buf_str[24];
    unsigned char buf_hex[24] = {0x55, 0xaa, 0x00, 0x03, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64};

    memset(buf_str,0,sizeof(buf_str));

    len = strlen((char *)buf_hex);
    printf("buf_hex len: %d\n",len);

    memcpy(buf_str,buf_hex + 5,0x0b);
    printf("buf_str: %s\n",buf_str);

	return 0;
}

strlen 的长度只有2,因为有个RAW的0x00,所以对于RAW和字符串共存的数据只能按照定义去解析

数据使用打印

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WangMing_GZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值