【问题标题】:storing and reading int data into char array将 int 数据存储和读取到 char 数组中
【发布时间】:2011-12-31 01:49:52
【问题描述】:

我正在尝试将两个整数值存储到 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的值。

我该怎么做??

谢谢, 尼克

P.S:我知道这是一种愚蠢的做法,但请相信我这是一种约束。

【问题讨论】:

    标签: c++ arrays char


    【解决方案1】:

    我还没有尝试过,但以下应该可以工作:

    char data[20];
    int value;
    
    memcpy(&value,data,sizeof(int));
    

    【讨论】:

      【解决方案2】:

      我认为当您编写 static_cast&lt;char&gt; 时,该值将转换为 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;
      

      【讨论】:

      • 或reinterpret_cast,如果你使用命名转换
      • C-Style casts 不应该再在 C++ 中使用,特别是如果您不清楚可能的副作用。
      【解决方案3】:
      #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);
      }
      

      【讨论】:

        【解决方案4】:

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

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

        只需交换= 的两侧,就可以使用相同的语法来读取data。

        但是请注意,此代码不可移植,因为在某些体系结构中,未对齐的操作可能不仅速度慢,而且实际上是非法的(崩溃)。 在这些情况下,最好的方法(如果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;
        

        【讨论】:

          【解决方案5】:

          尝试以下方法:

          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
              //...
          }
          

          使用这种联合应该可以消除对齐问题,除非将数据传递给具有不同架构的机器。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多