编码,一个整数解码到一个字符数组 [英] Encoding, decoding an integer to a char array

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

问题描述

请注意,这不是功课,我也开始这一新的线程之前搜索。我有<一个href=\"http://stackoverflow.com/questions/1522994/store-an-int-in-a-char-array\">http://stackoverflow.com/questions/1522994/store-an-int-in-a-char-array

我一直在寻找一个答案,但没有得到上面任何线程满意的答复。

下面是我的要求:我在一个字节数组要带code我的数据(例如一个整数),然后转移在网络上,然后去code的另一端并进行处理。

下面的编码部分:

  const int的MAX = 5;
uint32_t的一个= 0xff00ffaa;
烧焦BYTE_ARRAY [1024]; //这是通过网络传输的数组
字符的buff [MAX] =;
sprintf的(BUFF,4D%,一);
的memcpy(BYTE_ARRAY,浅黄色,4);
//填写剩余的东西字节数组中,并把它通过网络

下面是解码部分:

  const int的MAX = 5;
字符的buff [MAX] =;
strncat函数(BUFF,BYTE_ARRAY,4)INT I =的atoi(BUFF);
//与我的工作

下面是我的问题:

1)是上述code便携式?我估计是(请指正)

2)现在,我要带code中的字节数组3字节(但整数大小为4个),即说整店0x00ffaabb,我只是想将字节数组在FF INT第0指数AA第一个指数和bb在第二索引。那怎么办?

snprinf 似乎不工作或可能是我失去了一些东西。

谁实施了任何网络协议,可以很容易地帮助我的人。
解码逻辑将仍然工作我猜。 ( strncat函数(BUFF,BYTE_ARRAY,3)然后按的atoi 函数调用)。

下面是该协议说什么:


    -------- + -------- + -------- + -------- + -------------- ----------------
    |版本| 3字节长度|剩余的东西
    -------- + -------- + -------- + -------- + -------------- ----------------

版是1字节,随后该消息的3个字节的长度。

我希望能澄清我的问题


解决方案

您要存储为ASCII,在那里你应该存储字节本身。

编码应该是这样的:

  uint32_t的一个= 0xff00ffaa;
unsigned char型BYTE_ARRAY [1024];

注意我是如何使你的目标阵列符号,以表明它的原始字节,而没有实际的字符。

  BYTE_ARRAY [0] = A&GT;&GT; 24;
BYTE_ARRAY [1] = A&GT;&GT; 16;
BYTE_ARRAY [2] = A&GT;&GT; 8;
BYTE_ARRAY [3] = A&GT;&GT; 0;

本连载变量 A 为使用big-endian字节顺序,也就是四个第一字节 BYTE_ARRAY 的排序默认为许多网络协议。

您可能还希望在这里看到我的回答:<一href=\"http://stackoverflow.com/questions/1577161/passing-a-structure-through-sockets-in-c/1577174#1577174\">question 1577161 。

Please note that this is not homework and i did search before starting this new thread. I got http://stackoverflow.com/questions/1522994/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.

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

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