1、数值转成字符:
sprintf个函数,如: sprintf(s, "%d", 123); //产生"123"
2、字符串转成数值:
while(*BandRate='\0')
{num=num*10+(*BandRate-'0');
BandRate++;
}
char test_char[7]={0};
unsigned int cc=60000; //虽然都定义unsigned int
sprintf(test_char,"%u", cc); //输出的unsigned int的数字字符
sprintf(test_char,"%lu", cc); //输出的unsigned long的数字字符
sprintf(test_char,"%d", cc); //输出的signed int的数字 字符
sprintf(test_char,"%ld", cc); //输出的signed long的数字 字符
unsigned int un =3000000000;/*int为32位*/
short end = 200;
long big = 65537;
long long verybig = 12345678908642;
printf("un =%u and not %d \n",un,un);
printf("end =%hd and %d\n",end,end);
printf("big = %ld and %hd\n",big,big);
printf("verybig = %lld and %ld\n",verybig,verybig)
![](https://i-blog.csdnimg.cn/blog_migrate/b307170ceef8b0d5b6cb66258981d732.jpeg)
unsigned int ui = -1;
unsigned long ul = -1;
unsigned long long ull = -1;
size_t st = -1;
printf("ui=%u,ul=%lu,ull=%llu,st=%zu\n", ui, ul, ull, st);
位数比较
由于数据的长度和平台相关,所以基于 64 位系统比较。
Windows | Linux | |
---|---|---|
unsigned int | 32 bits/4294967295 | 32 bits |
unsigned long | 32 bits | 64 bits/18446744073709551615 |
unsigned long long | 64 bits | 64 bits |
size_t | 32 bits | 64 bits |
可以看到,size_t 和 unsigned long 比较同步。另外 ssize_t 和 size_t 的关系就像 int 和 unsigned int 的关系。(参考1,参考2)
格式控制(printf)
length | d i | u o x X | f F e E g G a A | c | s | p | n |
---|---|---|---|---|---|---|---|
(none) | int | unsigned int | double | int | char* | void* | int* |
hh | signed char | unsigned char | signed char* | ||||
h | short int | unsigned short int | short int* | ||||
l | long int | unsigned long int | wint_t | wchar_t* | long int* | ||
ll | long long int | unsigned long long int | long long int* | ||||
j | intmax_t | uintmax_t | intmax_t* | ||||
z | size_t | size_t | size_t* | ||||
t | ptrdiff_t | ptrdiff_t | ptrdiff_t* | ||||
L | long double |
#include <cstdio>
int main()
{
unsigned int ui = -1;
unsigned long ul = -1;