Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C字节数组转换为长字节数组_C_Arrays_Byte_Long Integer - Fatal编程技术网

将C字节数组转换为长字节数组

将C字节数组转换为长字节数组,c,arrays,byte,long-integer,C,Arrays,Byte,Long Integer,我的应用程序中有一个包含以下数据的8字节数组: 00000133339e36a2 此数据表示一个长值(在写入数据的平台上,在Mac上,这将是一个长值),值为 1319420966562 在实际应用中,这是一组半随机化的数据,因此数字总是不同的。因此,我需要将字节数组转换为可打印的long 我曾尝试将数据直接转换为长-长,但我想出了 1305392 我应该看到上面的数字 对于那些在C字节操作方面比我更有经验的人,我如何正确地将字节数组转换为长字节数组 编辑:奇怪的是,所有的解决方案都输出相同

我的应用程序中有一个包含以下数据的8字节数组:

00000133339e36a2
此数据表示一个长值(在写入数据的平台上,在Mac上,这将是一个长值),值为

1319420966562
在实际应用中,这是一组半随机化的数据,因此数字总是不同的。因此,我需要将字节数组转换为可打印的long

我曾尝试将数据直接转换为长-长,但我想出了

1305392
我应该看到上面的数字

对于那些在C字节操作方面比我更有经验的人,我如何正确地将字节数组转换为长字节数组

编辑:奇怪的是,所有的解决方案都输出相同的数字:866006690。这是数据最后四个字节的十进制等效值

long-num=0;
long long num = 0;
for(int i = 0; i < 8; i++) {
    num = (num << 8) | byte_array[i];
}
对于(int i=0;i<8;i++){
num=(num取决于您想要的端点,类似这样的方式可以工作:

int i;
char inArray[8] = { 0x00,0x00,0x01,0x33,0x33,0x9e,0x36,0xa2 };
long long outLong = 0;

for(i=0; i<8; i++)
    outLong |= ( (long long)inArray[i]) << (i*8) );
inti;
char inArray[8]={0x00,0x00,0x01,0x33,0x33,0x9e,0x36,0xa2};
long-outLong=0;
对于(i=0;i而言,这似乎是一种(罕见的)联合有用的情况:

union number
{
    char charNum[16];
    long long longNum;
};
复制(或仅使用)
le_long_long_数组。字节[]
用于数组; 读回
leu long\u long\u数组。longlong

例如:

fill_array(&le_long_long_array.byte[0]);
return le_long_long_array.longlong;
#include <stdint.h>
#include <stdio.h>

union le_long_long_array_u
{
  uint8_t byte[8];
  uint64_t longlong;
} le_long_long_array;

static uint8_t hex2int (char c)
{
    return (c >= '0' && c <= '9') 
            ? (uint8_t )(c  - '0')
            : ((c >= 'a' && c <= 'f') 
                ? (uint8_t )(c  - 'a')
                : ((c >= 'A' && c <= 'F') 
                    ? (uint8_t )(c  - 'A')
                    : 0));
}

int main (int argc, char **argv)
{
    if (argc == 2)
    {
        int i;
        for (i = 0; i <= 7; i++)
        {
            char *str = argv[1];

            if (str[2*i] == '\0' || str[2*i+1] == '\0')
            {
                printf("Got short string.\n");
                return 1;
            }

            le_long_long_array.byte[i] = (hex2int(str[2*i]) << 4) + hex2int(str[2*i+1]);
        }
        printf("Got %lld\n", le_long_long_array.longlong);
    }
    else
    {
        printf("Got %d args wanted 1.\n", argc - 1);
        return 1;
    }
    return 0;
}
更完整的例子:

fill_array(&le_long_long_array.byte[0]);
return le_long_long_array.longlong;
#include <stdint.h>
#include <stdio.h>

union le_long_long_array_u
{
  uint8_t byte[8];
  uint64_t longlong;
} le_long_long_array;

static uint8_t hex2int (char c)
{
    return (c >= '0' && c <= '9') 
            ? (uint8_t )(c  - '0')
            : ((c >= 'a' && c <= 'f') 
                ? (uint8_t )(c  - 'a')
                : ((c >= 'A' && c <= 'F') 
                    ? (uint8_t )(c  - 'A')
                    : 0));
}

int main (int argc, char **argv)
{
    if (argc == 2)
    {
        int i;
        for (i = 0; i <= 7; i++)
        {
            char *str = argv[1];

            if (str[2*i] == '\0' || str[2*i+1] == '\0')
            {
                printf("Got short string.\n");
                return 1;
            }

            le_long_long_array.byte[i] = (hex2int(str[2*i]) << 4) + hex2int(str[2*i+1]);
        }
        printf("Got %lld\n", le_long_long_array.longlong);
    }
    else
    {
        printf("Got %d args wanted 1.\n", argc - 1);
        return 1;
    }
    return 0;
}

正如您对小端数据的期望。

好问题!好答案!投入一分钱:

// Getting stuff "in"
unsigned long long ll1 = 1319420966562;
unsigned char * c = reinterpret_cast<unsigned char *>( &ll1 );

// Getting stuff "out"
unsigned long long ll2;
memcpy( &ll2, c, 8 );
//将内容“放入”
无符号长ll1=1319420966562;
无符号字符*c=重新解释强制转换(&ll1);
//把东西“拿出来”
无符号长ll2;
memcpy(和ll2,c,8);
ll2
现在等于
ll1


我必须管理,但是,我更喜欢
联合
示例。

也许是指针符号

uint8_t Array[8] = { 0x00,0x00,0x01,0x33,0x33,0x9e,0x36,0xa2 };
uint32_t *Long = Array;
或者如果可能的话

*((uint32_t*)Array)

&=不是正确的运算符。数组包含十六进制值。是的,我注意到了运算符并修复了它。似乎我误解了这个问题。也修复了这个问题,谢谢。请记住endianness,但我不知道它到底有多大。是的,我使用了前缀le,因为自年x86以来,Mac是小Endian在这种情况下,结构的用户有责任以适当的endian方式填写字节数组。我尝试了这个方法,最终得到了866006690号。有什么想法吗?很好的解决方案,还没有想到。