将 C 字节数组转换为 long long

发布于 2024-12-11 16:00:59 字数 465 浏览 0 评论 0原文

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

00000133339e36a2

该数据代表一个 long (在数据写入的平台上,在 Mac 上这将是一个 long long),其值为

1319420966562

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

我尝试过将数据直接转换为 long long,但我想出了

1305392

我应该在哪里看到上面的数字。

对于那些比我更有 C 字节操作经验的人来说,如何正确地将字节数组转换为 long long?

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

I have an 8-byte array in my application with this data:

00000133339e36a2

This data represents a long (on the platform the data was written in, on a Mac this would be a long long) with the value of

1319420966562

In the actual application this is a semi-randomized set of data, so the number will always be different. Therefore, I need to convert the byte array into a printable long long.

I've tried casting the data directly into a long long, but I came up with

1305392

where I should have been seeing the above number.

For those of you with more experience in C byte manipulation than I do, how would I correctly convert a byte array to a long long?

EDIT: Strangely, all of your solutions keep outputting the same number: 866006690. That is the decimal equivalent of the last four bytes of the data.

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

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

发布评论

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

评论(6

め七分饶幸 2024-12-18 16:00:59

这似乎是联合有用的(罕见)情况之一:

union number
{
    char charNum[16];
    long long longNum;
};

This seems one of the (rare) situations where an union is useful:

union number
{
    char charNum[16];
    long long longNum;
};
Smile简单爱 2024-12-18 16:00:59
union le_long_long_array_u
{
  uint8_t byte[8];
  uint64_t longlong;
} le_long_long_array;

复制(或仅使用)le_long_long_array.byte[] 作为数组;
回读le_long_long_array.longlong

例如:

fill_array(&le_long_long_array.byte[0]);
return le_long_long_array.longlong;

Fuller 示例:

#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;
}

产生:

e e$ gcc -c un.c
e e$ gcc -o un un.o
e e$ ./un 0100000000000000
Got 1
e e$ ./un 0101000000000000
Got 257
e e$ ./un a23639333301000000
Got 1319414347266
e e$ 

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

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

Copy (or just use) le_long_long_array.byte[] for the array;
read back le_long_long_array.longlong

For example:

fill_array(&le_long_long_array.byte[0]);
return le_long_long_array.longlong;

Fuller example:

#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;
}

Produces:

e e$ gcc -c un.c
e e$ gcc -o un un.o
e e$ ./un 0100000000000000
Got 1
e e$ ./un 0101000000000000
Got 257
e e$ ./un a23639333301000000
Got 1319414347266
e e$ 

as you would expect for little endian data.

若言繁花未落 2024-12-18 16:00:59
long long num = 0;
for(int i = 0; i < 8; i++) {
    num = (num << 8) | byte_array[i];
}

假设它们以大端存储。

long long num = 0;
for(int i = 0; i < 8; i++) {
    num = (num << 8) | byte_array[i];
}

Assuming they're stored big-endian.

失与倦" 2024-12-18 16:00:59

根据您想要的字节顺序,类似这样的事情会起作用:

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) );

Depending on your desired endianness, something like this would work:

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) );
蓝颜夕 2024-12-18 16:00:59

好问题!和很好的答案!顺便说一句:

// 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 );

ll2 现在等于 ll1

我必须管理,但是,我更喜欢 union 示例。 :)

Great question! And great answers! To throw in a cent:

// 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 );

ll2 is now equal to ll1.

I must admin, however, I like the union examples better. :)

鸢与 2024-12-18 16:00:59

也许是指针符号?

uint8_t Array[8] = { 0x00,0x00,0x01,0x33,0x33,0x9e,0x36,0xa2 };
uint32_t *Long = Array;

或者如果可能的话

*((uint32_t*)Array)

Perhaps a pointer notation?

uint8_t Array[8] = { 0x00,0x00,0x01,0x33,0x33,0x9e,0x36,0xa2 };
uint32_t *Long = Array;

Or if possible

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