標籤:
%6.2f
6表示資料表示至少6位,後面的.2表示小數點後保留兩位
比如2342.123415用這個表示的話,結果就是2342.12
如果不足六位就會在前面補空格
超過六位的話正常顯示
代碼例子:
int main()
{
float a=3425.1234;
printf("%6.2f",a);
return 0;
}
結果就是3425.12
如果a=5.1234
那結果就是: 5.12,前面有3個空格
如果a=234525.123
則結果過就是234525.12
1、前言
我們經常涉及到數字與字串之間的轉換,例如將32位不帶正負號的整數的ip地址轉換為點分十進位的ip地址字串,或者反過來。從給定的字串中提取相關內容,例如給定一個地址:http://www.bokeyuan.cn:2345,我們要從地址中提出協議,主機地址和連接埠號碼。之前對字串和數字之間的關係不是很熟悉,工作中經常涉及到這個,如是好好總結一下。C語言提供了一些列的格式化輸入輸出函數,最基本的是面向控制台標準輸出和輸入的printf和scanf,其實還有面向字串的sprint和sscanf,面向檔案的流的fprintf和fscanf。今天著重總結一下sprintf和sscanf系列函數,這兩個函數類似於scanf和printf ,不同點是從字串*buffer用於輸入輸出。
2、sprintf函數
sprintf函數原型為 int sprintf(char *str, const char *format, ...)。作用是格式化字串,具體功能如下所示:
(1)將數字變數轉換為字串。
(2)得到整型變數的16進位和8進位字串。
(3)串連多個字串。
舉例如下所示:
1 char str[256] = { 0 }; 2 int data = 1024; 3 //將data轉換為字串 4 sprintf(str,"%d",data); 5 //擷取data的十六進位 6 sprintf(str,"0x%X",data); 7 //擷取data的八進位 8 sprintf(str,"0%o",data); 9 const char *s1 = "Hello";10 const char *s2 = "World";11 //連接字串s1和s212 sprintf(str,"%s %s",s1,s2);
3、sscanf函數
sscanf函數原型為int sscanf(const char *str, const char *format, ...)。將參數str的字串根據參數format字串來轉換並格式化資料,轉換後的結果存於對應的參數內。具體功能如下:
(1)根據格式從字串中提取資料。如從字串中取出整數、浮點數和字串等。
(2)取指定長度的字串
(3)取到指定字元為止的字串
(4)取僅包含指定字元集的字串
(5)取到指定字元集為止的字串
sscanf可以支援格式字元%[]:
(1)-: 表示範圍,如:%[1-9]表示唯讀取1-9這幾個數字 %[a-z]表示唯讀取a-z小寫字母,類似地 %[A-Z]唯讀取大寫字母
(2)^: 表示不取,如:%[^1]表示讀取除‘1‘以外的所有字元 %[^/]表示除/以外的所有字元
(3),: 範圍可以用","相串連 如%[1-9,a-z]表示同時取1-9數字和a-z小寫字母
(4)原則:從第一個在指定範圍內的數字開始讀取,到第一個不在範圍內的數字結束%s 可以看成%[] 的一個特例 %[^ ](注意^後面有一個空格!)
解析網址的例子如下所示:
1 const char *s = "http://www.baidu.com:1234"; 2 char protocol[32] = { 0 }; 3 char host[128] = { 0 }; 4 char port[8] = { 0 }; 5 sscanf(s,"%[^:]://%[^:]:%[1-9]",protocol,host,port); 6 7 printf("protocol: %s\n",protocol); 8 printf("host: %s\n",host); 9 printf("port: %s\n",port);10
4、snprintf函數
snprintf函數是sprintf函數的更加安全版本,考慮到字串的位元組數,防止了字串溢出。函數形式為:int snprintf(char *restrict buf, size_t n, const char * restrict format, ...);。最多從源串中拷貝n-1個字元到目標串中,然後再在後面加一個0。所以如果目標串的大小為n 的話,將不會溢出。
5、測試程式
本次採用ip地址和整型之間的轉換,mac地址轉換作為測試程式,整個程式如下所示:
1 #include <stdio.h> 2 #include <assert.h> 3 4 #define IP_STR_LEN 18 5 #define MAC_STR_LEN 18 6 #define MAC_BIT_LEN 6 7 #define LITTLE_ENDIAN 0 8 #define BIG_ENDIAN 1 9 10 typedef unsigned char uchar;11 typedef unsigned int uint;12 13 int big_little_endian()14 {15 int data = 0x1;16 if (*((char*)&data) == 0x1)17 return LITTLE_ENDIAN;18 return BIG_ENDIAN;19 }20 21 uint ipstr2int(const char * ipstr)22 {23 assert(ipstr);24 uint a,b,c,d;25 uint ip = 0;26 sscanf(ipstr,"%u.%u.%u.%u",&a,&b,&c,&d);27 a = (a << 24) ;28 b = (b << 16) ;29 c = (c << 8) ;30 d = (d << 0) ;31 ip = a | b | c | d;32 return ip;33 }34 35 char *int2ipstr(const uint ip, char *ipstr, const uint ip_str_len)36 {37 assert(ipstr);38 if (big_little_endian() == LITTLE_ENDIAN)39 sprintf(ipstr,"%u.%u.%u.%u",40 (uchar)*((char*)(&ip)+3),41 (uchar)*((char*)(&ip)+2),42 (uchar)*((char*)(&ip)+1),43 (uchar)*((char*)(&ip)+0));44 else45 sprintf(ipstr,"%u.%u.%u.%u",46 (uchar)*((char*)(&ip)+0),47 (uchar)*((char*)(&ip)+1),48 (uchar)*((char*)(&ip)+2),49 (uchar)*((char*)(&ip)+3));50 51 return ipstr;52 }53 55 char *mac2str(const unsigned char *mac,char *mac_str,const uint mac_str_len)56 {57 assert(mac_str);58 sprintf(mac_str,"%02X-%02X-%02X-%02X-%02X-%02X",59 mac[0],mac[1],mac[2],60 mac[3],mac[4],mac[5]);61 }62 63 int main()64 {65 char ip_str[IP_STR_LEN] = {0};66 char mac_str[MAC_STR_LEN] = {0};67 unsigned char mac[MAC_BIT_LEN] = {0XEF,0XAD,0XF4,0X4F,0XAA,0X0F};68 const char *ipstr = "10.0.3.193";69 unsigned int ip;70 int2ipstr(167773121,ip_str,IP_STR_LEN);71 mac2str(mac,mac_str,MAC_STR_LEN);72 ip = ipstr2int(ipstr);73 printf("%s\n",ip_str);74 printf("%s\n",mac_str);75 printf("ip:%u\n",ip);76 return 0;77 }
程式執行結果如下所示:
參考網址:
http://www.360doc.com/content/08/0813/22/45933_1539152.shtml
http://blog.csdn.net/wesweeky/article/details/6439777
http://msdn.microsoft.com/en-us/library/ce3zzk1k.aspx
http://technet.microsoft.com/zh-tw/library/wez13747
http://orgcent.com/ip-address-integer-convert/
sprintf函數 %6.2f