user-0 提问: 如何将16进制的字符串“0x21”转换成字符型 char a=(cahr )0x21。

回复:
user: user-1
content: 引用 8 楼 zhangxiangDavaid 的回复:6楼的%i用的好 scanf Type Field Characters The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number.  Table R.8   Type Characters for scanf functions  Character  Type of Input Expected Type of Argument  c When used with scanf functions, specifies single-byte character; when used with wscanf functions, specifies wide character. White-space characters that are ordinarily skipped are read when c is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to char when used with scanf functions, pointer to wchar_t when used with wscanf functions.  C When used with scanf functions, specifies wide character; when used with wscanf functions, specifies single-byte character. White-space characters that are ordinarily skipped are read when C is specified. To read next non–white-space single-byte character, use %1s; to read next non–white-space wide character, use %1ws. Pointer to wchar_t when used with scanf functions, pointer to char when used with wscanf functions.  d Decimal integer. Pointer to int.  i Decimal, hexadecimal, or octal integer. Pointer to int.  o Octal integer. Pointer to int.  u Unsigned decimal integer. Pointer to unsigned int.  x Hexadecimal integer. Pointer to int.  e, E, f, g, G Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent (“e” or “E”) followed by an optionally signed integer value. Pointer to float.  n No input read from stream or buffer. Pointer to int, into which is stored number of characters successfully read from stream or buffer up to that point in current call to scanf functions or wscanf functions.  s String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed following Table R.7. When used with scanf functions, signifies single-byte character array; when used with wscanf functions, signifies wide-character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.  S String, up to first white-space character (space, tab or newline). To read strings not delimited by space characters, use set of square brackets ([ ]), as discussed preceding this table. When used with scanf functions, signifies wide-character array; when used with wscanf functions, signifies single-byte–character array. In either case, character array must be large enough for input field plus terminating null character, which is automatically appended.    The types c, C, s, and S are Microsoft extensions and are not ANSI-compatible.   Thus, to read single-byte or wide characters with scanf functions and wscanf functions, use format specifiers as follows.  To Read Character As Use This Function With These Format Specifiers  single byte scanf functions c, hc, or hC  single byte wscanf functions C, hc, or hC  wide wscanf functions c, lc, or lC  wide scanf functions C, lc, or lC    To scan strings with scanf functions, and wscanf functions, use the prefixes h and l analogously with format type-specifiers s and S.

user: user-2
content: char *s1="0x21";  int val = strtol(s1, NULL, 16); //16进制字符串,转换成整型.  printf("%d", val);

user: user-3
content: int main() { 	char *s1 = "0x21"; 	unsigned char c;     //为了防止溢出，使用unsigned 	sscanf(s1, "%x", &c); 	printf("c=%c,%d\n", c, c);  //!,33 	return 0; }

user: user-4
content: sscanf(str,"0x%x",&z);  感觉应该是这样，不过没试过。

user: user-3
content: 6楼的%i用的好

user: user-5
content: 应该有一个关于转成ASC码的函数吧

user: user-1
content: 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。

user: user-1
content: char *s1="0x21"; int i; char a; sscanf(s1,"%i",&i); a=(char)(i&0xffu) printf("%d\n",a);

user: user-3
content: 引用 4 楼 user-6 的回复:[quote=引用 3 楼 zhangxiangDavaid 的回复:]  int main() { 	char *s1 = "0x21"; 	unsigned char c;     //为了防止溢出，使用unsigned 	sscanf(s1, "%x", &c); 	printf("c=%c,%d\n", c, c);  //!,33 	return 0; }  这个sscanf的用法是错误的 sscanf(s1, "%hhx", &c);[/quote] 多谢指正！

user: user-6
content: 引用 3 楼 zhangxiangDavaid 的回复: int main() { 	char *s1 = "0x21"; 	unsigned char c;     //为了防止溢出，使用unsigned 	sscanf(s1, "%x", &c); 	printf("c=%c,%d\n", c, c);  //!,33 	return 0; }  这个sscanf的用法是错误的 sscanf(s1, "%hhx", &c);