%6.2f
6 means that the data represents at least 6 bits, followed by a. 2 indicating that two bits are reserved after the decimal point
For example, 2342.123415 uses this expression, the result is 2342.12.
If less than six bits will fill in the front space
More than six digits, normal display.
code example:
int main ()
{
float a=3425.1234;
printf ("%6.2f", a);
return 0;
}
The result is 3425.12.
If a=5.1234
The result is: 5.12, with 3 spaces in front of it.
If a=234525.123
The result is 234525.12.
1. Preface
We often involve conversions between numbers and strings, such as converting an IP address of a 32-bit unsigned integer to a dotted-decimal IP address string, or vice versa. Extracts related content from a given string, for example, given an address: http://www.bokeyuan.cn:2345, we want to propose a protocol, host address and port number from the address. Before the relationship between strings and numbers is not very familiar, the work often involves this, as a good summary. The C language provides some columns of formatted input and output functions, most fundamentally for the console standard output and input of printf and scanf, in fact there are string-oriented sprint and SSCANF, file-oriented stream fprintf and fscanf. Today we highlight the sprintf and SSCANF series functions, which are similar to scanf and printf, and differ from string *buffer for input and output.
2. sprintf function
sprintf function prototypes are int sprintf (char *str, const char *format, ...). The function is to format the string, as follows:
(1) Converts a numeric variable to a string.
(2) Get 16 binary and 8 binary strings of integer variables.
(3) Concatenate multiple strings.
An example is shown below:
1 char str[256] = {0}; 2 int data = 1024x768; 3 //Convert data to String 4 sprintf (str, "%d", data); 5 //Get hex of data 6 sprintf (str, "0x%X", data), 7 //Get Data of octal 8 sprintf (str, "0%o", data), 9 const char *S1 = "Hello"; Ten const char *S2 = "world"; //connection string S1 and s212 sprintf (str, "%s%s", S1,S2);
3. sscanf function
The SSCANF function is prototyped as int sscanf (const char *STR, const char *format, ...). The string of the parameter str is converted and formatted according to the parameter format string, and the converted result is stored in the corresponding parameter. The specific functions are as follows:
(1) Extracts data from a string according to the format. such as extracting integers, floating-point numbers, and strings from a string.
(2) Take a string of the specified length
(3) string to be taken to the specified character
(4) Take a string containing only the specified character set
(5) string to be taken to the specified character set
SSCANF can support format character%[]:
(1)-: Indicates a range, such as:%[1-9] means reading only 1-9 of these numbers%[a-z] means reading only a A-Z lowercase letter, similarly%[a-z] read only uppercase letters
(2) ^: Indicates not to take, such as:%[^1] means to read all characters except ' 1 '%[^/] denotes all characters except/
(3),: Range can be used "," connected such as%[1-9,a-z] means to take 1-9 numbers and a-Z lowercase letters
(4) Principle: Start reading from the first number within the specified range, to the end of the first number not in the range%s can be seen as a special case of%[]%[^] (note that there is a space behind!) )
Examples of parsing URLs are as follows:
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 function
The snprintf function is a more secure version of the sprintf function, taking into account the number of bytes in the string, preventing a string overflow. The function is:int snprintf (char *restrict buf, size_t N, const char * restrict format, ...);. Copy n-1 characters from the source string up to the target string, and then add a 0 to the back. So if the size of the target string is n, it will not overflow.
5. Test procedure
This time using the conversion between the IP address and the integer type, MAC address translation as a test program, the entire program is as follows:
1 #include <stdio.h> 2 #include <assert.h> 3 4 #define Ip_str_len 5 #define Mac_str_len 6 #defi NE mac_bit_len 6 7 #define Little_endian 0 8 #define Big_endian 1 9 typedef unsigned CHAR uchar;11 typedef uns igned int uint;12 int Big_little_endian () {int data = 0X1;16 if (* (char*) &data) = = 0x1) + retur N little_endian;18 return big_endian;19}20 uint Ipstr2int (const char * ipstr) (IPSTR) a,b,c,d;25 UINT IP = 0;26 sscanf (ipstr, "%u.%u.%u.%u", &a,&b,&c,&d); a = (a << 24); 28 b = (b <<); c = (c << 8); d = (d << 0); ip = a | B | C | d;32 return ip;33}34 char *int2ipstr (const UINT IP, char *ipstr, const UINT Ip_str_len) (IPSTR), 38 if (Big_little_endian () = = Little_endian) sprintf (ipstr, "%u.%u.%u.%u", + (UCHAR) * ((char*) (&IP) +3), 4 1 (UCHAR) * ((char*) (&IP) +2), 42 (UCHAR) * ((char*) (&IP) +1), (UCHAR) * ((char*) (&IP) +0)), Else45 sprintf (ipstr, "%u.%u.%u.%u", (UCHAR) * ((char*) (&IP) +0), (UCHAR) * ((char*) (&IP) +1), (UCHAR) * ((char*) (&IP) +2), 49 (UCHAR) * ((char*) (&IP) +3)); return ipstr;52}53-char *mac2str (const unsigned char *mac,char *mac_s Tr,const uint Mac_str_len) (MAC_STR), sprintf (Mac_str, "%02x-%02x-%02x-%02x-%02x-%02x", Mac [0],mac[1],mac[2],60 mac[3],mac[4],mac[5]);}62 Int. int main () () () () () () () () () () (): {0};66 ch Ar 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"; unsigned int ip;70 int2ipstr (167773121,ip_str,ip_str_len); Mac2str (mac,mac_str,mac_s Tr_len); ip = ipstr2int (IPSTR); ("%s\n", ip_str); printf ("%s\n", mac_str); printf ("ip:%u\n", IP ); return 0;77}
The program execution results are as follows:
Reference URL:
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 function%6.2f