Integral type and string conversion to each other

Source: Internet
Author: User
Tags 04x

When we write the program, we often encounter the problem of integer type and string conversion, here to use a few functions, itoa (), Atoi (), sprintf () below to introduce the specific use of these functions!

Itoa

Function: Converts an integer to a string
Usage: char *itoa (int value, char *string, int radix);
Explanation: ITOA is an abbreviation for the English Integer to array (converting int integer to a string and storing the value in the array string).
Parameters:
Value: the integer to convert.
Radix: Is the meaning of the cardinality, that is, the value is converted to the number of radix, the range is between 2-36, for example, 10 means 10, 16 means 16 binary.
* String: The string to be saved after the conversion.
return value:
char *: Points to the generated string, with *string.
Note: The function's header file is "Stdlib.h"
program Example:
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int number = 123456;
Char string[25];
Itoa (number, string, 10);
printf ("integer =%d string =%s\n", number, string);
return 0;
}
Comment: Compile system: VC++6.0,TC not supported. atoiC Language Library function name: atoi
Function: Converts a string to an integer number.
Name Source: an abbreviation for array to integer.
Function Description: Atoi () scans the parameter nptr string, if the first character is not a number and is not the sign returns zero, otherwise the type conversion is started, then a non-numeric or terminator is detected to stop the conversion, returning the integer number.
Prototype: int atoi (const char *nptr);
Header files to use: #include <stdlib.h>
program Example:
1)
#include <stdlib.h>
#include <stdio.h>
int main ()
{
int n;
Char *str = "12345.67";
n = atoi (str);
printf ("string =%s integer =%d\n", str, n);
return 0;
}
Execution results
string = 12345.67 integer = 12345
2)
#include <stdlib.h>
#include <stdio.h>
int main ()
{
Char a[] = "100";
Char b[] = "123";
int C;
c = Atoi (a) + atoi (b);
printf ("c =%d\n", c);
return 0;
}
Execution results
C = 23
sprintfsprintf to format the string.
In the header file #include <stdio.h>
Syntax: string sprintf (string format, mixed [args] ...);
return value: String
1. Handle the character orientation. -The minus sign when the table is processed from the back forward.
2. Fill in the blanks. 0 words means blank 0, space is the default value, indicating that the space is placed.
3. The total width of the characters. To the minimum width.
4. Accuracy. Refers to the number of floating-point digits after the decimal point.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Convert characters
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% printed percent symbol, not converted.
b integer turns into binary.
The c integer is converted to the corresponding ASCII character.
D integers are turned into 10.
The F-Times precision number is converted to floating point numbers.
o integers are turned into octal.
The s integer is converted into a string.
The x integer is converted to lowercase 16 rounding.
X integers are converted to uppercase 16 rounding.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
<?
$money = 123.1
$formatted = sprintf ("%06.2f", $money); At this time the variable $ formatted value is "123.10"
$formatted = sprintf ("%08.2f", $money); At this time the variable $ formatted value is "00123.10"
$formatted = sprintf ("%-08.2f", $money); At this time the variable $ formatted value is "123.1000"
$formatted = sprintf ("%.2f%%", 0.95 * 100); Format as Percent
?>
¢%08.2f Explanation:% start character
0 is the "fill in the blanks" means that if the length is not enough to fill with a.
6 total length after formatting
2f decimal digit length, 2-bit
¢ the 4th row value is "00123.10" explained:
Because 2f is (2 bits) + decimal sign (1) + Front 123 (3 bit) = 6 bit, total length is 8 bits, so the front is used in [fill in the blank character]0, that is, 00123.10
¢ the 4th row value is "123.1000" explained:
-Number for the reverse operation, then fill in the blanks 0 added in the last face
  
The power of sprintf rarely disappoints you when constructing various types of data into strings. Since sprintf is almost the same as printf in usage, it is printed in a different destination, which prints to a string, and the latter is output directly on the command line. This also causes sprintf to be much more useful than printf.
sprintf is a variable parameter function, which is defined as follows:
int sprintf (char *buffer, const char *format [, argument] ...);
In addition to the first two parameter types are fixed, can be followed by any number of parameters. And its essence, obviously, is in the second parameter:
The formatted string.
Both printf and sprintf use a formatted string to specify the format of the string, using some format specifier (the form specifications) that begins with "%" in the format string to occupy a position, providing the corresponding variable in the list of arguments behind it. The final function replaces that specifier with a variable of the corresponding position, producing a string that the caller wants.
Formatting numeric strings
One of the most common applications of sprintf is to print integers into strings, so SPRITNF can be substituted for most occasions
Itoa
Such as:
The integer 123 is printed into a string stored in S.
sprintf (S, "%d", 123); Generate "123"
You can specify a width, insufficient left fill space:
sprintf (S, "%8d%8d", 123, 4567); Generated: "123 4567"
Of course, you can also align Left:
sprintf (S, "%-8d%8d", 123, 4567); Generated: "123 4567"
You can also print in 16 binary:
sprintf (S, "%8x", 4567); lowercase 16 Binary, Width 8 positions, right justified
sprintf (S, "%-8x", 4568); Uppercase 16 binary, Width 8 positions, left justified
In this way, an integer 16 binary string is easy to get, but when we are printing 16 binary content, we usually want a kind of equal width format of 0 on the left. It's easy to add a 0 to the front of the number that represents the width.
sprintf (S, "%08x", 4567); Produce: "000011d7"
The 10-binary printing above with "%d" can also use this way to the left to fill 0.
Note the problem with a symbol extension: For example, if we want to print a 16 binary representation of a short integer-1 of the memory, on the Win32 platform, a shorter type is 2 bytes, so we naturally want to print it with 4 16 digits:
Short si =-1;
sprintf (S, "%04x", si);
"FFFFFFFF", what's going on? Because SPRITNF is a variable parameter function, in addition to the previous two parameters, the following parameters are not type-safe, the function is not able to just through a "%x" can be learned that the first function call before the argument is pressed into the stack when the end is a 4-byte integer or a 2-byte short integer, so took a unified 4 byte processing mode, resulting in the parameter stack when the symbol expansion, expanded into 32-bit integer-1, when printing 4 position is not enough, the 32-bit integer 1 8-bit 16 is printed out.
If you want to see the true nature of SI, then you should let the compiler do 0 extensions instead of the symbol extension (the binary left side of the extension is 0 instead of the fill sign bit):
sprintf (S, "%04x", (unsigned short) SI);
You can do it. Or:
unsigned short si =-1;
sprintf (S, "%04x", si);
sprintf and printf can also print integer strings in 8 binary, using "%o". Note that 8 binary and 16 binary are not hit
Printed negative numbers are unsigned, which is actually the direct 16-or 8-binary representation of the internal code of the variable.
Control floating-point printing format
Floating-point printing and format control is another common function of sprintf, floating-point numbers use the format "%f" control, the default
Leave 6 digits after the decimal point, for example:
sprintf (S, "%f", 3.1415926); Generate "3.141593"
But sometimes we want to control the width and scale of the print, then we should use: "%M.NF" format, where M-table
Shows the width of the print, and n indicates the number of digits after the decimal point. Like what:
sprintf (S, "%10.3f", 3.1415626); Generated: "3.142"
sprintf (S, "%-10.3f", 3.1415626); Generated: "3.142"
sprintf (S, "%.3f", 3.1415626); Does not specify the total width, resulting in: "3.142"
Pay attention to a question, you guess.
int i = 100;
sprintf (S, "%.2f", I);
What's going to happen? "100.00"? Is that right? Try it yourself, and try the following:
sprintf (S, "%.2f", (double) i);
The first one is definitely not the right result, as mentioned earlier, the caller does not know that the format controller corresponding to I is a "%f" when the parameter is pressed. When the function is executed, the function itself does not know that the year was pressed into the stack is an integer, so poor to save the integer I of the 4 bytes is without any explanation forcibly as a floating-point format to explain, the whole mess. However, if someone is interested in using manually coding a floating-point number, then you can use this method to verify that the results of your manual orchestration are correct.
Character/ascii Code Control
As we know, char is also a common type of scalable in the C + + language, with short, in addition to the word length,
Int,long These types have no essential difference, but are used by people to represent characters and strings. (Perhaps it was time to
This type is called "byte" and can now be used to define char through a typedef using byte or short as the case may be, so that it is more appropriate to use "%d" or "%x" to print a character, and then to derive its 10-or 16-binary ASCII code In turn, using "%c" to print an integer, you can see the ASCII character it corresponds to. The following section prints the ASCII Code table of all visible characters to the screen (in this case, printf, note that the "#" and "%x" Automatically add the "0X" prefix to the 16 binary number):
for (int i = +; i < 127; i++) {
printf ("[%c]:%3d 0x% #04X \ n", I, I, I);
}
Connection string
In the sprintf format control string, since it is possible to insert a variety of things and eventually "connect" them, it is natural to be able to connect
strings, which in many cases can be substituted for strcat, but sprintf can concatenate multiple strings at once (naturally
Insert something else between them, in short, very flexible. Like what:
char* who = "I";
char* whom = "CSDN";
sprintf (S, "%s love%s.", who, whom); Produce: "I love CSDN." "
Strcat can only concatenate strings (an array of characters ending in ' ' or ' character buffers, null-terminated-string), but sometimes we have two-bit buffers, and they don't end with '. For example, many of the character arrays that are returned from third-party library functions, the stream of characters that are read from hardware or network transmissions, do not necessarily end with a corresponding ' at the back of each sequence of characters. If the direct connection, whether it is sprintf or strcat will certainly lead to illegal memory operations, and Strncat also at least require the first parameter is a null-terminated-string, then what to do? We naturally recall that the previous introduction of printing integers and floating-point numbers can specify the width, and the string is the same. Like what:
Char a1[] = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G '};
Char a2[] = {' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N '};
If:
sprintf (S, "%s%s", a1, A2); Don ' t do that!
There's going to be a problem. Whether it can be changed to:
sprintf (S, "%7s%7s", a1, A2);
There is no better place to go, the right should be:
sprintf (S, "%.7s%.7s", a1, A2);//generated: "ABCDEFGHIJKLMN"
This can be analogous to the "%m.nf" of the print floating-point number, in "%m.ns", where M is the occupied width (when the length of the string is insufficient to fill a space, beyond the actual width of the print), n represents the maximum number of characters to be taken from the corresponding string. Usually when I print a string, M is not much use, or the number behind the dot is used more. Naturally, you can only take part of the characters before and after:
sprintf (S, "%.6s%.5s", a1, A2);//generated: "ABCDEFHIJKL"
In many cases, we may also want the numbers in these format controls to specify length information to be dynamic, rather than statically specified, because many times the program will have to run until it is clear that a few characters in the character array need to be taken, and this dynamic width/precision setting function is sprintf is also taken into account, sprintf uses "*" to occupy a constant number where a specified width or precision is required, and the actual width or precision can be provided as the other printed variables, and the above example can be turned into:
sprintf (S, "%.*s%.*s", 7, A1, 7, A2);
Or:
sprintf (S, "%.*s%.*s", sizeof (A1), A1, sizeof (A2), a2);
In fact, the previously described print characters, integers, floating-point numbers, and so on can be dynamically specified those constant values, such as:
sprintf (S, "%-*d", 4, ' A '); Generate "65"
sprintf (S, "% #0 *x", 8, 128); Generate "0X000080", "#" to produce 0X
sprintf (S, "%*.*f", 10, 2, 3.1415926); Generate "3.14"
Print address information
Sometimes when debugging a program, we may want to look at the address of some variable or member, and because the address or pointer is just a 32-bit number, you can print them out using the "%u" that prints the unsigned integer:
sprintf (S, "%u", &i);
But usually people prefer to use 16 binary instead of 10 to display an address:
sprintf (S, "%08x", &i);
However, these are indirect methods, for address printing, SPRINTF provides a dedicated "%p":
sprintf (S, "%p", &i);
I think it's actually equivalent to:
sprintf (S, "%0*x", 2 * sizeof (void *), &i);
take advantage of the return value of sprintf
few people notice the return value of the printf/sprintf function, but sometimes it is useful, SPRITNF returns the function call
The number of characters that are ultimately printed to the character buffer. That is, you do not need to call again once the Sprinf call is finished.
Strlen already knows the length of the resulting string. Such as:
int len = sprintf (S, "%d", I);
for a positive integer, Len is equal to the number of 10 decimal digits of the integer i.
The following is a complete example that produces a random number between 10 [0, 100) and prints them into a character array s,
separated by commas.
#include
#include
#include
int main () {
Srand (Time (0));
Char s[64];
int offset = 0;
for (int i = 0; i < i++) {
offset + = sprintf (s + offset, "%d,", rand ()% 100);
}
s[offset-1] = ' \ n ';//replace the last comma with a newline character.
printf (s);
return 0;
}
imagine that when you take a record out of the database and then want to connect their fields to a single word in a certain rule
string, you can use this method theoretically, he should be more efficient than the constant strcat, because strcat each call
need to find the last position, and in the example given above, we use the sprintf return value each time to
a place to write it down directly. (Say this paragraph I did not understand, first sent up to study it ~ ~ ~)
Frequently asked questions about using sprintf
sprintf is a variable parameter function, often problematic when used, and as long as the problem is usually a memory visit that can cause the program to crash
Ask the wrong, but fortunately by the misuse of sprintf caused by serious problems, but it is easy to find out, is nothing more than a few cases, through
Often eyes can see the wrong code more than a few eyes.
?? Buffer overflow
The length of the first parameter is too short to say, give a bigger place. Of course, it could be the argument behind the question
, it is recommended to be careful with the argument, while printing the string, try to specify the maximum number of characters in the form "%.ns".
?? Forget the first parameter
Low-level can not be any more low-level problems, with printf used too accustomed. I often make it. :-)
?? The change parameter corresponds to the problem
Usually forget to provide a corresponding format for a variable parameter, resulting in the subsequent parameter dislocation, check it. Especially
Are those parameters corresponding to "*" provided? Do not match an integer to a "%s", and the compiler will think you
Bullying her too far (compiler is the mother of obj and EXE, should be a female,:P).
Strftime
SPRNITF also has a good cousin: Strftime, dedicated to formatting the time string, the use of her cousin very much like, also
is a lot of format control, just after all small roomed cautious, she also wants the caller to specify the maximum length of the buffer, possibly for
You can shirk your responsibility in the event of a problem. Here's an example:
time_t t = time (0);
A string that produces a "yyyy-mm-dd hh:mm:ss" format.
Char s[32];
Strftime (S, sizeof (s), "%y-%m-%d%h:%m:%s", LocalTime (&t));
Sprintf can also find his bosom friend in MFC: Cstring::format,Strftime in MFC naturally also has her fellow: Ctime::format,The pair has been sponsored from object-oriented, and the code used to write it is more elegant.

Integral type and string conversion to each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.