将整数编码、解码为 char 数组 [英] Encoding, decoding an integer to a char array

查看:41
本文介绍了将整数编码、解码为 char 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,这不是家庭作业,我在开始这个新线程之前进行了搜索.我得到了 将 int 存储在 char 数组中?

我一直在寻找答案,但在上面的帖子中没有得到任何满意的答案.

这是我的要求:我想将我的数据(比如一个整数)编码为一个字节数组,然后通过网络传输,然后在另一端解码并处理它.

这里是编码部分:

const int MAX=5;uint32_t a = 0xff00ffaa;字符字节数组[1024];//这是要通过网络传输的数组字符缓冲区 [MAX]="";sprintf(buff,"%4d",a);memcpy(byte_array,buff,4);//填充字节数组中剩余的内容并通过网络发送

这是解码部分:

const int MAX=5;字符缓冲区 [MAX]="";strncat(buff,byte_array,4)int i=atoi(buff);//与 i 一起工作

这是我的问题:

1) 上述代码是否可移植?我猜是(请纠正我)

2) 现在,我希望用 3 个字节(但整数大小为 4)对字节数组进行编码,即整数存储 0x00ffaabb,我只希望字节数组在第一个索引中有 ff int 第 0 个索引 aa 和bb 在第二个索引中.该怎么做?

snprinf 似乎不起作用,或者我可能遗漏了什么.

实现过任何网络协议的人都可以轻松帮助我.我猜解码逻辑仍然有效.(strncat(buff,byte_array,3) 后跟 atoi 函数调用).

协议如下:

<上一页>--------+--------+--------+--------+------------------------------|版本|3 字节长度 |剩下的东西--------+--------+--------+--------+------------------------------

版本为 1 字节,后跟 3 字节长度的消息.

我希望我能澄清我的问题

解决方案

你存储为 ASCII,你应该存储字节本身.

编码应该是这样的:

uint32_t a = 0xff00ffaa;无符号字符字节数组[1024];

注意我是如何让你的目标数组无符号的,以表明它是原始字节",而不是真正的字符.

byte_array[0] = a>>24;byte_array[1] = a>>16;byte_array[2] = a>>8个;byte_array[3] = a>>0;

这使用大端字节顺序将变量 a 序列化为 byte_array 的前四个字节,这是许多网络协议的默认设置.

您可能还想在此处查看我的回答:问题1577161.

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array?

I was looking for an answer but didn't get any satisfactory answer in the above thread.

Here's my requirement: I want to encode my data(say an integer) in a byte array and then transfer over the network and then decode at the other end and process it.

Here's the encoding part:

const int MAX=5;
uint32_t a = 0xff00ffaa;
char byte_array[1024]; // this is the array to be transferred over the network
char buff[MAX]="";
sprintf(buff,"%4d",a);
memcpy(byte_array,buff,4);
// fill remaining stuff in the byte array and send it over the network

Here's the decoding part:

const int MAX=5;
char buff[MAX]="";
strncat(buff,byte_array,4)

int i=atoi(buff);
// Work with i

Here are my questions :

1) Is the above code portable? I guess it is( please correct me)

2) Now, I wish to encode the byte array with 3 bytes (but the integer size is 4) i.e say the integer stores 0x00ffaabb and i just want the byte array to have ff int 0th index aa in the 1st index and bb in the 2nd index. How to do that?

snprinf doesn't seem to work or may be i am missing something.

A person who has implemented any network protocol can easily help me out. Decoding logic would still work i guess. (strncat(buff,byte_array,3) followed by atoi function call).

Here's what the protocol says :

    --------+--------+--------+--------+------------------------------
    |Version|   3       byte    length |  Remaining stuff
    --------+--------+--------+--------+------------------------------

Version is 1 byte, followed by 3 byte length of the message.

I hope I could clarify my problem

解决方案

You're storing as ASCII, where you should be storing the bytes themselves.

The encoding should be something like:

uint32_t a = 0xff00ffaa;
unsigned char byte_array[1024];

Notice how I made your target array unsigned, to indicate that it's "raw bytes", and not actually characters.

byte_array[0] = a >> 24;
byte_array[1] = a >> 16;
byte_array[2] = a >> 8;
byte_array[3] = a >> 0;

This serializes the variable a into the four first bytes of byte_array using big-endian byte ordering, which is sort of the default for many network protocols.

You may also want to see my answer here: question 1577161.

这篇关于将整数编码、解码为 char 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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