In depth analysis of C++ function itoa of
- 2020-04-02 00:58:47
- OfStack
The function itoa() is a function that converts an integer type toa c-style string
The prototype:
Char * itoa (int data, char*p, int num);
Data is the incoming converted number, is the integer variable (the maximum value of data is 2 to the 31st power minus 1), p is the incoming character pointer, pointing to the first address of the converted string space; Num specifies the string of digits to be converted to (binary, octal, decimal, hexadecimal).
If there are shortcomings, also hope to point out!!
Char * itoa (int data, char*p, int num);
Data is the incoming converted number, is the integer variable (the maximum value of data is 2 to the 31st power minus 1), p is the incoming character pointer, pointing to the first address of the converted string space; Num specifies the string of digits to be converted to (binary, octal, decimal, hexadecimal).
If there are shortcomings, also hope to point out!!
//TestInheritance. CPP: defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int myItoa(int data, char* p, int num)
{
if (p == NULL)
{
return -1;
}
if (data < 0)
{
*p++ = '-';
data = 0 - data;
}
int temp = 0;
int flag = 0; //Flag bit 0- does not store 1- stores
if (num == 10)
{//The decimal system
for (int i = 0; i < 10; i++)
{
temp = static_cast<int>(data / pow(10.0, 9-i));//Pow of I,j, I to the j, temp takes the current highest bit
if (temp != 0) //We get rid of the first 0
{
flag = 1;//Changes the flag bit to 1 and can be stored
}
if (flag != 0)
{
*p++ = temp + '0'; //Become character
data = data % static_cast<int>(pow(10.0, 9-i));
}
}
}
else if (num == 2)
{//binary
for (int i = 0; i < 32; i++)
{
temp = static_cast<int>(data / pow(2.0, 31-i)); //Int, Max. (2 ^ 31 -1),
if (temp != 0)
{
flag = 1;
}
if (flag != 0)
{
*p++ = temp + '0';
data = data % static_cast<int>(pow(2.0, 31 - i));
}
}
}
else if (num == 16)
{//hexadecimal
for (int i = 0; i < 8; i++)
{
temp = static_cast<int>(data / pow(16.0, 7-i));
if (temp != 0)
{
flag = 1;
}
if (flag != 0)
{
if (temp >= 0 && temp <= 9)
{
*p++ = temp + '0';
}
else if (temp >= 10 && temp <= 15)
{
*p++ = temp - 10 + 'A';
}
data = data % static_cast<int>(pow(16.0, 7 - i));
}
}
}
else if (num == 8)
{//octal
for (int i = 0; i < 16; i++)
{
temp = static_cast<int>(data / pow(8.0, 15-i));
if (temp != 0)
{
flag = 1;
}
if (flag != 0)
{
*p++ = temp + '0';
data = data % static_cast<int>(pow(8.0, 15-i));
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i = 100;
char a[32] ={0};
char b[32] ={0};
char c[32] ={0};
char d[32] ={0};
cout << i << " the octal Represented as a : ";
myItoa(i, a, 8);
cout << a << endl;
cout << i << " the The decimal system Represented as a : ";
myItoa(i, b, 10);
cout << b << endl;
cout << i << " the binary Represented as a : ";
myItoa(i, c, 2);
cout << c << endl;
cout << i << " the hexadecimal Represented as a : ";
myItoa(i, d, 16);
cout << d << endl;
return 0;
}