1.字符串转整数
函数:int atoi(const char *nptr);
头文件:#include <stdlib.h>
2.整数转字符串
函数:int sprintf(char *str, const char *format, …);
头文件:#include <stdio.h>
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main(int argc, char **argv) { char buf[10]; int num =99; int n = 199; printf("%s\n", buf); sprintf(buf, "%d %d", num, n); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { int num = 0; if ( argc < 2 ) { printf("Usage: d2hex <Decimal number>\n"); return 1; } printf("Hex: 0x%x\n", atoi(argv[1])); return 0; } |