用printf打印字符串十六进制格式,扭曲的结果 [英] use printf to print character string in hex format, distorted results

查看:1136
本文介绍了用printf打印字符串十六进制格式,扭曲的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印字符串十六进制格式,

I want to print character string in hex format,

在机器A,像

ori_mesg = gen_rdm_bytestream (1400,seed)
sendto(machine B, ori_mesg, len(mesg))

在机器B

 recvfrom(machine A, mesg)

mesg_check = gen_rdm_bytestream (1400, seed)

            for(i=0;i<20;i++){
                    printf("%02x ", *(mesg+i)& 0xFF);
            }
            printf("\n");
            for(i=0;i<20;i++){
                    printf("%02x ", *(mesg_check+i));
            }
            printf("\n");

种子变化中1,2 3 ....

seed varies among 1, 2 3....

字节代funcion是:

the bytes generation funcion is:

u_char *gen_rdm_bytestream (size_t num_bytes, unsigned int seed)
{
    u_char *stream = malloc (num_bytes+4);
    size_t i;

    u_int16_t seq = seed;
    seq = htons(seq);
    u_int16_t tail = num_bytes;
    tail = htons(tail);
    memcpy(stream, &seq, sizeof(seq));
    srand(seed);
    for (i = 3; i < num_bytes+2; i++){
            stream[i] = rand ();
            }
    memcpy(stream+num_bytes+2, &tail, sizeof(tail));

  return stream;
}

但我得到的结果从printf的,如:

but I got results from printf like:

00 01 00 67 c6 69 73 51 ff 4a ec 29 cd ba ab f2 fb e3 46 7c 
00 01 00 67 ffffffc6 69 73 51 ffffffff 4a ffffffec 29 ffffffcd ffffffba ffffffab fffffff2 fffffffb ffffffe3 46 7c

00 02 88 fa 7f 44 4f d5 d2 00 2d 29 4b 96 c3 4d c5 7d 29 7e 
00 02 00 fffffffa 7f 44 4f ffffffd5 ffffffd2 00 2d 29 4b ffffff96 ffffffc3 4d ffffffc5 7d 29 7e 

为什么有那么多的 FFFFF mesg_check

有没有这种现象任何潜在的原因是什么?
谢谢!

are there any potential reasons for this phenomenon? thanks!

推荐答案

下面是说明我的问题想的你可能有一个小程序:

Here's a small program that illustrates the problem I think you might be having:

#include <stdio.h>
int main(void) {
    char arr[] = { 0, 16, 127, 128, 255 };
    for (int i = 0; i < sizeof arr; i ++) {
        printf(" %2x", arr[i]);
    }
    putchar('\n');
    return 0;
}

在我的系统(在其纯字符签署),我得到这样的输出:

On my system (on which plain char is signed), I get this output:

  0 10 7f ffffff80 ffffffff

255 ,当存储在一个(签字)字符,存储为 1 。在的printf 通话,它的晋升(签字) INT - 但% 2X格式告诉的printf 把它当作一个 unsigned int类型,所以它显示 fffffffff

The value 255, when stored in a (signed) char, is stored as -1. In the printf call, it's promoted to (signed) int -- but the "%2x" format tells printf to treat it as an unsigned int, so it displays fffffffff.

请确保您的 MESG mesg_check 数组被定义为无符号的字符数组,而不是普通的字符

Make sure that your mesg and mesg_check arrays are defined as arrays of unsigned char, not plain char.

更新:超过一年后重读这个答案,我意识到这是不完全正确。下面是我的系统上正常工作的程序,并且将任何合理的系统上几乎可以肯定工作:

UPDATE: Rereading this answer more than a year later, I realize it's not quite correct. Here's a program that works correctly on my system, and will almost certainly work on any reasonable system:

#include <stdio.h>
int main(void) {
    unsigned char arr[] = { 0, 16, 127, 128, 255 };
    for (int i = 0; i < sizeof arr; i ++) {
        printf(" %02x", arr[i]);
    }
    putchar('\n');
    return 0;
}

输出是:

 00 10 7f 80 ff

类型的参数 unsigned char型被提升为(签字) INT (假设 INT 可容纳类型的所有值 unsigned char型,即 INT_MAX&GT; = UCHAR_MAX ,它是在几乎所有的系统的情况下)。因此,参数改编[I] 提升为 INT ,而%02X 格式需要类型的参数 unsigned int类型

An argument of type unsigned char is promoted to (signed) int (assuming that int can hold all values of type unsigned char, i.e., INT_MAX >= UCHAR_MAX, which is the case on practically all systems). So the argument arr[i] is promoted to int, while the " %02x" format requires an argument of type unsigned int.

C标准强烈暗示,但不的非常的状态直接对应符号​​和无符号类型的参数是可以互换的,只要它们是两种类型的范围之内 - 这是这里的情况。

The C standard strongly implies, but doesn't quite state directly, that arguments of corresponding signed and unsigned types are interchangeable as long as they're within the range of both types -- which is the case here.

要成为的完全的正确,你需要确保的说法实际上是类型 unsigned int类型

To be completely correct, you need to ensure that the argument is actually of type unsigned int:

printf("%02x", (unsigned)arr[i]);

这篇关于用printf打印字符串十六进制格式,扭曲的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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