C将2字节转换为短字节,反之亦然
我想将字节数组C将2字节转换为短字节,反之亦然,c,type-conversion,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 *)
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;
inti=0;
int j=0;
字符*字节1;
char*bytes2;
短*短整数;
bytes1=(char*)malloc(2048);
bytes2=(char*)malloc(2048);
short_ints=(short*)malloc(2048);
对于(i=0;i嗯,你需要的是一个联合体:
#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;
}
#包括
#包括
米肖特联合酒店{
短_值;
结构{
字符字节1;
字符字节2;
};
};
int main(int argc,const char*argv[]
{
字符a[4]=“abcd”;
字符b[4]=“1234”;
短c[5];c[4]=0;
联合米肖特d;
对于(int i=0;i建议使用2个函数。将所有组合和提取都作为无符号进行,以消除short
和char
中符号位的问题
符号位是OP的代码最大的问题。short\u int[j]=bytes1[i+1]>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;
}
避免了并集
方法,因为这是依赖于平台端的。下面的程序演示了您遇到的与位移位有符号整数值相关的问题
#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;
}
它通过以下方式进行测试:
char bytes1[4] = {-10, 0, 25, -4};
及
它们有何不同?对于较小的输入,预期和实际的输出是什么?输入不小;它是2048字节。许多元素不同,它们都在1000以上的索引中。在1000以下,所有元素都相等。您似乎左移到符号位并将其移回。这是实现定义的,可能是符号位也是移位的。根据经验,总是使用无符号类型进行逐位运算。因为在使用bytes1
数组之前没有初始化它,所以您不知道将得到什么。不,您不想使用有符号字节;您将在某个时候强制转换为unsigned
。在我看来,OP需要的是小尾数,而不是平台的持久性。是的,您已经找到了原因。但是为什么不修改代码使其工作呢?不管这些其他解决方案
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};