C;将2个字节转换为short,反之亦然

时间:2014-08-22 01:17:47

标签: c type-conversion

我想将字节数组bytes1(小端),2乘2转换为短整数数组,反之亦然。我希望得到最终数组bytes2,等于初始数组bytes1。我有这样的代码:

  int i = 0;
  int j = 0;

  char *bytes1;
  char *bytes2;
  short *short_ints;

  bytes1 = (char *) malloc( 2048 );
  bytes2 = (char *) malloc( 2048 );
  short_ints = (short *) malloc( 2048 );

  for ( i=0; i<2048; i+=2)
   {
     short_ints[j] = bytes1[i+1] << 8 | bytes1[i] ;
     j++;
   }

  j = 0;

  for ( i=0; i<2048; i+=2)
     {
        bytes2[i+1] = (short_ints[j] >> 8)  & 0xff;
        bytes2[i] = (short_ints[j]) ;
        j++;
     }
  j = 0;

现在,有人可以告诉我为什么我没有bytes2数组,与bytes1完全相同?以及如何正确地做到这一点?

3 个答案:

答案 0 :(得分:1)

建议2个功能。将所有组合和提取作为无符号进行,以删除short中的符号位和char中的符号位的问题。

符号位是OP代码最大的问题。 short_ints[j] = bytes1[i+1] << 8 | bytes1[i] ;可能会在bytes1[i]转换为int的情况下延伸一个符号   此外,(short_ints[j] >> 8)会延长一个标志。

// Combine every 2 char (little endian) into 1 short
void charpair_short(short *dest, const char *src, size_t n) {
  const unsigned char *usrc = (const unsigned char *) src;
  unsigned short *udest = (unsigned short *) dest;
  if (n % 2) Handle_OddError();
  n /= 2;
  while (n-- > 0) {
    *udest = *usrc++;
    *udest += *usrc++ * 256u;
    udest++;
  }
}

// Break every short into 2  char (little endian)
void short_charpair(char *dest, const short *src, size_t n) {
  const unsigned short *usrc = (const unsigned short *) src;
  unsigned char *udest = (unsigned char *) dest;
  if (n % 2) Handle_OddError();
  n /= 2;
  while (n-- > 0) {
    *udest++ = (unsigned char) (*usrc);
    *udest++ = (unsigned char) (*usrc / 256u);
    usrc++;
  }
}

int main(void) {
  size_t n = 2048;  // size_t rather than int has advantages for array index

  // Suggest code style: type *var = malloc(sizeof(*var) * N);
  // No casting of return
  // Use sizeof() with target pointer name rather than target type.
  char *bytes1 = malloc(sizeof * bytes1 * n);
  Initialize(bytes, n); //TBD code for OP-best to not work w/uninitialized data

  // short_ints = (short *) malloc( 2048 );
  // This is weak as `sizeof(short)!=2` is possible

  short *short_ints = malloc(sizeof * short_ints * n/2);
  charpair_short(short_ints, bytes1, n);

  char *bytes2 = malloc(sizeof * bytes2 * n);
  short_charpair(bytes2, short_ints, n);

  compare(bytes1, bytes2, n); // TBD code for OP

  // epilogue 
  free(bytes1);
  free(short_ints);
  free(bytes2);
  return 0;
}

避免采用union方法,因为它依赖于平台端。

答案 1 :(得分:1)

这是一个程序,用于演示您遇到与位移有符号整数值相关的问题。

#include <stdio.h>
#include <stdlib.h>

void testCore(char bytes1[],
              char bytes2[],
              short short_ints[],
              int size)
{
   int i = 0;
   int j = 0;

   for ( i=0; i<size; i+=2)
   {
      short_ints[j] = bytes1[i+1] << 8 | bytes1[i] ;
      j++;
   }

   j = 0;

   for ( i=0; i<size; i+=2)
   {
      bytes2[i+1] = (short_ints[j] >> 8)  & 0xff;
      bytes2[i] = (short_ints[j]) ;
      j++;
   }

   for ( i=0; i<size; ++i)
   {
      if ( bytes1[i] != bytes2[i] )
      {
         printf("%d-th element is not equal\n", i);
      }
   }
}

void test1()
{
   char bytes1[4] = {-10, 0, 0, 0};
   char bytes2[4];
   short short_ints[2];
   testCore(bytes1, bytes2, short_ints, 4);
}

void test2()
{
   char bytes1[4] = {10, 0, 0, 0};
   char bytes2[4];
   short short_ints[2];
   testCore(bytes1, bytes2, short_ints, 4);
}

int main()
{
   printf("Calling test1 ...\n");
   test1();
   printf("Done\n");
   printf("Calling test2 ...\n");
   test2();
   printf("Done\n");
   return 0;
}

计划的输出:

Calling test1 ...
1-th element is not equal
Done
Calling test2 ...
Done

<强> UDATE

这是适用于我的testCore版本:

void testCore(char bytes1[],
              char bytes2[],
              short short_ints[],
              int size)
{
   int i = 0;
   int j = 0;
   unsigned char c1;
   unsigned char c2;
   unsigned short s;

   for ( i=0; i<size; i+=2)
   {
      c1 = bytes1[i];
      c2 = bytes1[i+1];
      short_ints[j] = (c2 << 8) | c1;
      j++;
   }

   j = 0;

   for ( i=0; i<size; i+=2)
   {
      s = short_ints[j];
      s = s >> 8;
      bytes2[i+1] = s;
      bytes2[i] = short_ints[j] & 0xff;
      j++;
   }

   for ( i=0; i<size; ++i)
   {
      if ( bytes1[i] != bytes2[i] )
      {
         printf("%d-th element is not equal\n", i);
      }
   }
}

经测试:

char bytes1[4] = {-10, 0, 25, -4};

char bytes1[4] = {10, -2, 25, 4};

答案 2 :(得分:0)

嗯,你需要的是一个UNION:

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

union MyShort {
    short short_value;
    struct {
        char byte1;
        char byte2;
    };
};

int main(int argc, const char * argv[])
{
    char a[4]="abcd";
    char b[4]="1234";
    short c[5]; c[4]=0;
    union MyShort d;
    for (int i = 0; i<4; i++) {
        d.byte1 = a[i];
        d.byte2 = b[i];
        c[i] = d.short_value;
    }//next i
    printf("%s\n", (char*)c);
    return 0;
}

结果应为a1b2c3d4。