十六进制数对于 long long int 来说太大?

发布于 2024-12-19 17:33:59 字数 651 浏览 4 评论 0原文

希望您能在这里帮助我:)

我已经订购了 RFID 标签和扫描仪供业余爱好使用。该标签正在将输入发送到我想在 C 程序中使用的标签。

问题是..标签正在发送十六进制代码 - 10 个字符 - 长。

我试图将这个数字存储在一个 long long int 变量中,但它似乎不起作用。

当我使用 printf 打印变量时,仅打印前 8 个字符。其余的(省略 2 位数字)。

long long int n1;
n1 = recieve_data();
printf("%lx", n1);

我也尝试自己输入 10 位十六进制数字,执行以下操作:

long long int n1;
scanf("%lx", &n1);
printf("%lx", n1);

我输入 10 位数字 0 到 F,但只打印 8 位数字/字符。

一定存在某种整数溢出,但据我所知:

十六进制中的 1 位 = 4 位。那么 10 位十六进制数 = 40 位? - long long int 应该足够了!

如果我的直觉是正确的,并且发生了整数溢出,那么我该如何解决这个问题?显然没有足够大的变量来保存 10 位十六进制值?

此致 尼古拉斯 - 丹麦

Hope you can help me out here :)

I've ordered a RFID-tag and scanner for hobby use. The tag is sending input to tag, that I want to use in a C-program.

The problem is.. the tag is sending a code in hexadecimal - 10 characters - long.

I've tried to store this number in a long long int variable, but it doesn't seem to work.

When i print variable using printf, only the first 8 characters are printed. The rest (2 digits are left out).

long long int n1;
n1 = recieve_data();
printf("%lx", n1);

also i've tried to input 10 digit hexadecimal number myself, doing:

long long int n1;
scanf("%lx", &n1);
printf("%lx", n1);

I enter 10 digits 0 to F, but only 8 digits / characters are printed.

There must be some kind of integer overflow, but as far as I know:

1 digit in hex = 4 bit. Then 10 digits in hex = 40 bits ? - long long int should be sufficient !

If my instincts are right, and there is happening integer overflow, then how can I fix this ? Apparently no variable is large enough to hold the 10 digit hex value ?

Best regards
Nicolas - Denmark

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

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

发布评论

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

评论(5

苏大泽ㄣ 2024-12-26 17:33:59

您确定您的 long long int 是 64 位宽,您的环境是嵌入式、PC、32 位还是 64 位?编译器?

如果您的编译器像 gcc 一样兼容 C99,您还可以使用 stdint.huint64_t ,它们保证独立于平台为 64 位宽。

您应该重新检查您的 printf() 文档,例如,在我的 64 位 OpenSuse 框中,使用 gcc,long 以及 long lont int 均为 64通过 sizeof() 位宽。要打印它们,我使用:

printf(" %llx ",hexNumber)

Are you sure that your long long int is 64 bit wide, what i your environment, embedded, PC, 32 or 64 bit ? Compiler?

If your compiler is C99 compliant like gcc you can also use stdint.h and uint64_t which is guaranteed independent of platform to be 64 bits wide.

Your should recheck your printf() documentation, For example in my 64 bit OpenSuse box with gcc a long as well as long lont int are 64 bit wide via sizeof(). To print them I use:

printf(" %llx ",hexNumber)

咿呀咿呀哟 2024-12-26 17:33:59

10个十六进制字符只有40位。 long long int 保证至少为 64 位,因此适合您的使用。

问题实际上出在 printf 格式字符串中 - %lxunsigned long int 的正确格式字符串,而不是 long long int。要打印long long int,您应该使用%lld - 如果您想以十六进制打印它,您应该将其声明为unsigned long long int > 而是使用 %llx

10 hexadecimal characters is only 40 bits. long long int is guaranteed to be at least 64 bits, so it is fine for your use.

The problem is actually in your printf format string - %lx is the right format string for an unsigned long int, not a long long int. To print a long long int you should use %lld - if you want to print it in hexadecimal, you should declare it as unsigned long long int instead, and use %llx.

别靠近我心 2024-12-26 17:33:59

您正在使用 %lx 进行扫描和打印。

据我了解,格式字符串 %lx 是一个 long int,而不是一个 long long int。根据您的硬件,您可能只能获得 32 位的 long int。

其他人之一建议使用 %llx,表示 long long int。尝试一下也没什么坏处。

You are scanning and printing using %lx.

As I understand format strings %lx is a long int, not a long long int. Depending on your hardware, you may only get 32 bits for a long int.

One of the other guys suggested %llx, for long long int. It can't hurt to try that.

话少心凉 2024-12-26 17:33:59

将数字放入管理大小的部分并将其存储为字符串。这样就无限长了。

Bring the number in in management sized parts and store it as a string. Infinite length that way.

橙味迷妹 2024-12-26 17:33:59

标签正在发送十六进制代码 - 10 个字符

我很确定您的意思是“标签正在将 RFID 作为 10 个字符的 ASCII 字符串发送,仅限于 16 个十六进制数字”

我尝试将这个数字存储在一个 long long int 变量中,

为什么

int 及其朋友都是算术类型。您可以使用它们来存储计划执行算术运算的值。您可以对它们进行递增、添加 73、向左位移 3、按位或或将它们用作索引。

但 RFID 只是一个名称。你不会对一个名字做任何这些事情,因为这样做没有意义。你不会说“kermit the frog”+8,或“miss Piggy”<<7或“fozzie bear”%3。 RFID 也是如此。您只需读入 RFID,比较它们,也许对它们进行哈希处理,然后将它们写出来。它们是不可分割的、难以理解的“神奇饼干”字符串——它们只是标记。

所以你应该将它存储为字符串。将其读入 int,然后从 int 写回字符串不仅会产生两个不必要的转换,而且在语义上毫无意义。

the tag is sending a code in hexadecimal - 10 characters

I'm pretty sure you mean "the tag is sending the RFID as a 10 character ASCII string, confined to the 16 hexadecimal digits"

I've tried to store this number in a long long int variable,

why?

An int, and its friends, are arithmetic types. You use them to store values you plan to perform arithmetic on. You increment them, add 73 to them, bitshift them 3 to the left, bitwise OR them, or use them as an index.

But an RFID is just a name. You don't do any of these things to a name, because it isn't meaningful to do so. You wouldn't say "kermit the frog"+8, or "miss piggy"<<7 or "fozzie bear"%3. So it is with RFIDs. You just read RFIDs in, compare them, maybe hash on them, and write them out. They are indivisible, inscrutable, "magic-cookie" strings - they're just tokens.

So you should store it as a string. Not only does reading it into an int and then writing it back out to a string from an int generate two unnecessary convertions, it's semantically meaningless.

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