将 int 数据存储和读取到 char 数组中

发布于 2024-12-16 21:27:59 字数 404 浏览 0 评论 0原文

我正在尝试将两个整数值存储到 C++ 中的 char 数组中。 这是代码。

char data[20];
*data = static_cast <char> (time_delay);   //time_delay is of int type
*(data + sizeof(int)) = static_cast<char> (wakeup_code);  //wakeup_code is of int type

现在在程序的另一端,我想反转此操作。也就是说,从这个char数组中,我需要获取time_delay和wakeup_code的值。

我怎样才能做到这一点?

谢谢, Nick

P.S:我知道这是一种愚蠢的方法,但相信我,这是一个限制。

I am trying to store two integer value into an char array in C++.
Here is the code..

char data[20];
*data = static_cast <char> (time_delay);   //time_delay is of int type
*(data + sizeof(int)) = static_cast<char> (wakeup_code);  //wakeup_code is of int type

Now on the other end of the program, I want to reverse this operation. That is, from this char array, I need to obtain the values of time_delay and wakeup_code.

How can I do that??

Thanks,
Nick

P.S: I know this is a stupid way to do this, but trust me its a constraint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事还在继续 2024-12-23 21:27:59

我认为当您编写 static_cast 时,该值会转换为 1 字节字符,因此如果它不适合开始时的字符,您将丢失数据。

我要做的是使用 *((int*)(data+sizeof(int)))*((int*)(data+sizeof(int))) 用于读取和写入整数到数组。

*((int*)(data+sizeof(int))) = wakeup_code;
....
wakeup_code = *((int*)(data+sizeof(int)));

或者,你也可以这样写:

reinterpret_cast<int*>(data)[0]=time_delay;
reinterpret_cast<int*>(data)[1]=wakeup_code;

I think when you write static_cast<char>, that value is converted to a 1-byte char, so if it didn't fit in a char to begin with, you'll lose data.

What I'd do is use *((int*)(data+sizeof(int))) and *((int*)(data+sizeof(int))) for both reading and writing ints to the array.

*((int*)(data+sizeof(int))) = wakeup_code;
....
wakeup_code = *((int*)(data+sizeof(int)));

Alternatively, you might also write:

reinterpret_cast<int*>(data)[0]=time_delay;
reinterpret_cast<int*>(data)[1]=wakeup_code;
高速公鹿 2024-12-23 21:27:59

如果您使用的是 PC x86 架构,则不存在对齐问题(速度除外),您可以将 char * 转换为 int * 来进行转换:

char data[20];
*((int *)data) = first_int;
*((int *)(data+sizeof(int))) = second_int;

并且只需交换 = 的两边即可使用相同的语法来读取 data

但请注意,此代码不可移植,因为在某些体系结构中,未对齐的操作可能不仅很慢,而且实际上是非法的(崩溃)。
在这些情况下,最好的方法可能是在代码中显式构建整数,一次一个字符:

first_uint = ((unsigned char)data[0] |
              ((unsigned char)data[1] << 8) |
              ((unsigned char)data[2] << 16) |
              ((unsigned char)data[3] << 24));
data[4] = second_uint & 255;
data[5] = (second_uint >> 8) & 255;
data[6] = (second_uint >> 16) & 255;
data[7] = (second_uint >> 24) & 255;

If you are working on a PC x86 architecture then there are no alignment problems (except for speed) and you can cast a char * to an int * to do the conversions:

char data[20];
*((int *)data) = first_int;
*((int *)(data+sizeof(int))) = second_int;

and the same syntax can be used for reading from data by just swapping sides of =.

Note however that this code is not portable because there are architectures where an unaligned operation may be not just slow but actually illegal (crash).
In those cases probably the nicest approach (that also gives you endianness control in case data is part of a communication protocol between different systems) is to build the integers explicitly in code one char at a time:

first_uint = ((unsigned char)data[0] |
              ((unsigned char)data[1] << 8) |
              ((unsigned char)data[2] << 16) |
              ((unsigned char)data[3] << 24));
data[4] = second_uint & 255;
data[5] = (second_uint >> 8) & 255;
data[6] = (second_uint >> 16) & 255;
data[7] = (second_uint >> 24) & 255;
绿萝 2024-12-23 21:27:59

我还没有尝试过,但以下应该有效:

char data[20];
int value;

memcpy(&value,data,sizeof(int));

I haven't tried it, but the following should work:

char data[20];
int value;

memcpy(&value,data,sizeof(int));
吹梦到西洲 2024-12-23 21:27:59

尝试以下操作:

union IntsToChars {
struct {
int time_delay;
int wakeup_value;
} Integers;
char Chars[20];
};

extern char* somebuffer;

void foo()
{
    IntsToChars n2c;
    n2c.Integers.time_delay = 1;
    n2c.Integers.wakeup_value = 2;
    memcpy(somebuffer,n2c.Chars,sizeof(n2c));  //an example of using the char array containing the integer data
    //...
}

使用此类联合应该可以消除对齐问题,除非数据传递到具有不同体系结构的机器。

Try the following:

union IntsToChars {
struct {
int time_delay;
int wakeup_value;
} Integers;
char Chars[20];
};

extern char* somebuffer;

void foo()
{
    IntsToChars n2c;
    n2c.Integers.time_delay = 1;
    n2c.Integers.wakeup_value = 2;
    memcpy(somebuffer,n2c.Chars,sizeof(n2c));  //an example of using the char array containing the integer data
    //...
}

Using such union should eliminate the alignment problem, unless the data is passed to a machine with different architecture.

小瓶盖 2024-12-23 21:27:59
#include <sstream>
#include <string>
int main ( int argc, char **argv) {
    char ch[10];
    int i = 1234;

    std::ostringstream oss;
    oss << i;
    strcpy(ch, oss.str().c_str());

    int j = atoi(ch);
}
#include <sstream>
#include <string>
int main ( int argc, char **argv) {
    char ch[10];
    int i = 1234;

    std::ostringstream oss;
    oss << i;
    strcpy(ch, oss.str().c_str());

    int j = atoi(ch);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文