çUINT16如何得到它的权利? [英] C UINT16 How to get it right?

查看:158
本文介绍了çUINT16如何得到它的权利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C编程新我测试了一些code,其中我接收和处理的格式如下一个UDP包:

I'm new on C programming and I'm testing some code where I receive and process an UDP packet formatted as follow:

UINT16 port1
UINT16 port2

在此测试的相应的值是:

The corresponding values on this test are:

6005
5555

如果我打印整个数据包缓冲区我得到的是这样的:

If I print the whole packet buffer I get something like this:

ü^ ^W³U>< 9e中GT; ^ D

所以,我以为我会只是要打破它和工艺为 unsigned int类型 16个字节。所以,我已经试过这样的事情:

So I thought that I would just have to break it and process as an unsigned int of 16 bytes. So I've tried something like this:

int l = 0;
unsigned int *primaryPort = *(unsigned int) &buffer[l]; 
AddToLog(logInfo, "PrimaryPort: %u\n", primaryPort);
l += sizeof(primaryPort);
unsigned int *secondaryPort = *(unsigned int) &buffer[l]; 
AddToLog(logInfo, "SecondaryPort: %u\n", secondaryPort);
l += sizeof(secondaryPort);

但我得到错误的数字与8位数字。

But I get wrong numbers with 8 digits.

我甚至尝试像后续另一种方法,但也得到了错误的号码为好。

I even tried another approach like follow, but also get the wrong number as well.

int l = 0;
unsigned char primaryPort[16]; 
snprintf(primaryPort, sizeof(primaryPort), "%u", &buffer[l]);
AddToLog(logInfo, "PrimaryPort: %d\n", primaryPort);
l += sizeof(primaryPort);
unsigned char secondaryPort[16]; 
snprintf(secondaryPort, sizeof(secondaryPort), "%u", &buffer[l]);
AddToLog(logInfo, "SecondaryPort: %d\n", secondaryPort);
l += sizeof(secondaryPort);

我在做什么错?另外,为什么我必须释放对字符字符串变量,但我并不需要释放对INT变量?

What I'm doing wrong? Also, why I have to free on a char string variables, but I don't need to free on int variables?

推荐答案

您传递给 AddToLog 的snprintf 指针的整数。所以,你看到的是整数的地址,而不是整数本身。

You are passing to AddToLog and snprintf pointers to the integers. So what you're seeing are the addresses of the integers, not the integers themselves.

您需要取消引用您的指针 - 例如,把一个星号(*)的 primaryPort 的前面您来电 AddToLog 在你的第一种方法。

You need to dereference your pointers -- for example, put an asterisk (*) in front of primaryPort in your calls to AddToLog in your first approach.

由于@rileyberton指出,最有可能的 unsigned int类型为你的系统,这是C99型4个字节 uint32_t的。对于16位整数,使用 uint16_t 。这些在 stdint.h 定义。这些都是传统上被称为短整型或半整数,并要求在%胡适预选赛的printf 或类似功能,而不仅仅是%U (表示 unsigned int类型,其大小取决于在目标机器上。)

As @rileyberton suggests, most likely unsigned int is 4 bytes on your system, which is the C99 type uint32_t. For a 16-bit integer, use uint16_t. These are defined in stdint.h. These are traditionally called "short integers" or "half integers" and require the %hu qualifier in printf or similar functions, rather than just %u (which stands for unsigned int, whose size depends on the target machine.)

此外,如@伊戈尔 - tandetnik建议,您可能需要切换你的数据包中的整数的字节顺序,例如,如果该数据包是使用网络顺序(big-endian)的格式和目标计算机使用little- endian格式。

Also, as @igor-tandetnik suggests, you may need to switch the byte order of the integers in your packet, if for example the packet is using network order (big-endian) format and your target machine is using little-endian format.

这篇关于çUINT16如何得到它的权利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆