Objective
Integer, floating-point and string conversion can be implemented with the function of their own, I use vs2015, so the following function needs to be changed, see the following summary.
Body
First, the integer type to the string type
1. int to string
itoa (int _value, char *_buffer, int _radix);
need to be changed to _itos_s (int _value, char *_buffer, size_t _buffercount, int _radix);
_radix: Indicates the binary, if the decimal is Radix 10
_buffercount: The size of the storage can not be larger than the length of buffer
#include <iostream>using namespace Std; void Main () { int num = 10 ; char str[3 ]; // Str has a minimum length of 3 because 10, although two bits, has a terminator _itoa_s (num, str, sizeof (str), 10 ); // cout << str << Endl; // output is }
2. long int to string
Ltoa (Long _value, char *_buffer, int _radix);
Change to _ltoa_s (long _value, Char *_buffer, size_t _buffercount, int _radix);
3. unsigned long integer converted to string
Ultoa (unsigned long _value, char *_buffer, int _radix);
Change to _ultoa_s (unsigned long _value, char *_buffer, size_t _buffercount, int _radix);
4. int type to wide string type
_itow (int _value, wchar_t *_buffer, int _radix);
Change to _itow_s (int _value, wchar_t *_buffer, size_t _buffercount, int _radix);
Second, float type to String type
1. Double type to String
GCVT (double _value, int _digitcount, char* _dstbuf);
Change to _gcvt_s (char* _buffer, size_t _buffercount, double _value, int _digitcount);
Description: Displays the sign, the decimal point
_digitcount: The number of digits shown, such as 1.25, shows that two bits are 1.3 (rounded), showing three bits is 1.25
#include <iostream>using namespace Std; void Main () { double num =-1.235 ; char str[7 ]; // _gcvt_s (str, sizeof (str), NUM, 3 ); // double to string, display three bits, 1.235 rounded to 1.24 cout << str << Endl; // output is -1.24 }
2. Double conversion to a string
ECVT (double _value, int _digitcount, int *_ptdec, int *_ptsign);
Change to _ecvt_s (char *_buffer, size_t _buffercount, double _value, int _digitcount, int *_ptdec, int *_ptsign);
Description: Do not display decimal and sign
_ptdec: Indicates the decimal point position, if _PTDEC is 1, indicating a number to the left of the decimal point
_ptsign: Indicates positive sign, 0 is positive, 1 is negative
#include <iostream>using namespacestd;voidMain () {Doublenum =-1.235; intDEC, sign;//Dec: Decimal position, sign: positive and negative Charstr[5];//? At least 5 people_ecvt_s (str,sizeof(str), NUM,3, &dec, &sign);//double to str, minus sign and decimal point, show three bits, 12,354 five into 124cout << str << Endl;//output is 124cout << Dec << Endl;//the output is 1 and there is a number to the left of the decimal pointcout << sign << Endl;//output is 1, negative}
3. Double conversion to a string
FCVT (double _value, int _fractionaldigitcount, int *_ptdec, int *_ptsign);
Change to _FCVT (char *_buffer, size_t _buffercount, double _value, int _fractionaldigitcount, int *_ptdec, int *_ptsign);
Description: The conversion result does not include the decimal point and the plus sign
_fractionaldigitcount: Take the number of decimal places, if the _fractionaldigitcount is 1, then take a decimal place, to be rounded
#include <iostream>using namespacestd;voidMain () {Doublenum =-1.235; intDEC, sign;//Dec: Decimal position, sign: positive and negative Charstr[5];//? At least 5 people_fcvt_s (str,sizeof(str), NUM,2, &dec, &sign);//double to str, minus sign and decimal point, take two decimal places, 12,354 five into 124cout << str << Endl;//output is 124cout << Dec << Endl;//the output is 1 and there is a number to the left of the decimal pointcout << sign << Endl;//output is 1, negative}
Three, the string type to turn the integral type
1. Convert string to int type
int atoi (const char *_string);
2. Converting a string to a long type
Long Atol (const char *_string);
3. Convert the string to long and report all remaining parts that cannot be converted
Long strtol (const char *_string, char **_endptr, int _radix);
_radix: Indicates a binary, range of 2~36 and 0
_endptr: Pointing to an illegal part of a string
Note: If _radix is 2, then ' 0 ', ' 1 ' is legal, if _radix is 10, then ' 0 ', ' 1 ' ... legal, if _radix is 9, then ' 16 ', ' 0 ' ... ' f ' legal
#include <iostream>using namespacestd;voidMain () {Longnum_2, Num_8, num_10, num_16; Charstr[ -] ="1079aeg"; Char*str1; Num_2= Strtol (str, &STR1,2);//Binary , 10 legalcout << num_2 << Endl;//Output 2, binary 10 is 2 in decimalcout << str1 << Endl;//the output is not legal 79aegNum_8 = Strtol (str, &STR1,8);//8 Binary, 107 legalcout << num_8 << Endl;//output 71, octal 107 in decimalcout << str1 << Endl;//the output is not legal 9aegNUM_10 = Strtol (str, &STR1,Ten);//10 binary, 1079 legalcout << num_10 << Endl;//Output 1079cout << str1 << Endl;//export of illegal AEFnum_16 = Strtol (str, &STR1, -);//16 binary, 1079ae legalcout << num_16 << Endl;//output 1079726, hex 1079ae is 1079726 in decimalcout << str1 << Endl;//the output is illegal g}
------
To learn more about the function, see Strtol ()
4. Convert the string to an unsigned long value and report all remaining parts that cannot be converted
unsigned long strtoul (const char *_string, char **_endptr, int _radix);
Four, string type to float type
1. Convert a string to a double-precision floating-point value
Double atof (const char *_string);
2. Converts a string to a double-precision floating-point value and reports all remaining digits that cannot be converted
Double strtod (const char *_string, char **_endptr);
Reference C language Itoa () function and atoi () function (integer to character C implementation)About conversion of type int,float,double to string char*Strtol () detailed
C + + integer, floating-point, and string-matching conversions