【发布时间】:2019-04-12 20:18:52
【问题描述】:
我正在编写 C 代码,我想将某个字符的十六进制值与另一个进行比较。这是可行的,但是对于像� 这样的某些值,我在 4 个字节上获得了一个值,这在我的比较中毫无意义。
void obs_text(char* start, unsigned char debugLevel, unsigned char depth){
printf("%c -> 0x%x\n", start[0], start[0]);
}
我希望输出包含两个十六进制字符,但实际输出是 ? -> 0xffffffef。
请问有人明白会发生什么吗?感谢您的帮助。
我正在用 gcc 编译。
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.3)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
但我也尝试在 Debian 操作系统上遇到同样的问题
【问题讨论】:
-
我们需要更多信息。你能提供调用那个函数的代码吗?
-
请注意,
start[0]在传递给可变参数函数时会提升为int。如果您传递的char是负数,那么int也将是负数,然后%x会将其视为无符号值。 -
尝试
printf("%c -> %d\n", start[0], start[0]);并报告输出。
标签: c