简体   繁体   English

十六进制的Scanf unsigned char

[英]Scanf unsigned char in hex

I am trying to change data in array, this is part of my code: 我正在尝试更改数组中的数据,这是我的代码的一部分:

u_char paket[100];
//here i put some data into array and then trying to change it by user
scanf("%hhx.%hhx.%hhx.%hhx.%hhx.%hhx", &paket[0], &paket[1], &paket[2], &paket[3], &paket[4], &paket[5]);

When my input is for example 88.88.88.88.88.88 it sets paket[0] - paket[5] to 88, but it also changes paket[6], paket[7] and paket[8] to 0. 当我的输入是例如88.88.88.88.88.88它将paket[0] - paket[5]为88,但它也将paket[6], paket[7]paket[8]更改为0。

How is it possible and how to fix it please? 怎么可能,怎么解决呢? I need to change only [0] - [5] 我只需改变[0] - [5]

Your code is correct for C99 and later. 您的代码对于C99及更高版本是正确的。 Presumably you are using a C standard library that does not support the hh length modifier, which was introduced in C99; 据推测,您使用的C标准库不支持在C99中引入的hh长度修饰符; probably the Microsoft C standard library. 可能是Microsoft C标准库。

If you need to support this old C standard library, you will have to rewrite your code to be C89-compatible, for example: 如果您需要支持这个旧的C标准库,则必须将代码重写为C89兼容,例如:

unsigned p[6];

if (scanf("%x.%x.%x.%x.%x.%x", &p[0], &p[1], &p[2], &p[3], &p[4], &p[5]) == 6)
{
    int i;
    for (i = 0; i < 6; i++)
        paket[i] = p[i];
}

Your code is fine 你的代码很好

u_char paket[100];
scanf("%hhx.%hhx.%hhx.%hhx.%hhx.%hhx", &paket[0], &paket[1], &paket[2], &paket[3], &paket[4], &paket[5])

Does not change the values of paket[6], paket[7] and paket[8] . 不改变paket[6], paket[7] and paket[8]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM