深入C++實(shí)現(xiàn)函數(shù)itoa()的分析
更新時(shí)間:2013年05月29日 11:43:33 作者:
本篇文章是對(duì)C++實(shí)現(xiàn)函數(shù)itoa()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
函數(shù)itoa()是將整數(shù)型轉(zhuǎn)換為c語(yǔ)言風(fēng)格字符串的函數(shù),原型:
char * itoa(int data, char*p, int num);
data是傳入的帶轉(zhuǎn)化的數(shù)字,為整型變量(data的最大值為2的31次方減去1),p是傳入的字符型指針,指向存儲(chǔ)轉(zhuǎn)換后字符串空間的首地址;num指定要轉(zhuǎn)換成幾進(jìn)制的數(shù)字字符串(二進(jìn)制,八進(jìn)制,十進(jìn)制,十六進(jìn)制)。
如有不足之處,還望指正!?。?BR>
// TestInheritance.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#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; //標(biāo)志位 0-不存儲(chǔ) 1-存儲(chǔ)
if (num == 10)
{//十進(jìn)制
for (int i = 0; i < 10; i++)
{
temp = static_cast<int>(data / pow(10.0, 9-i));// pow(i,j),求i的j次方,temp取得當(dāng)前最高位
if (temp != 0) //去掉最前面的0
{
flag = 1;//將標(biāo)志位變?yōu)?,可以存儲(chǔ)
}
if (flag != 0)
{
*p++ = temp + '0'; //變成字符
data = data % static_cast<int>(pow(10.0, 9-i));
}
}
}
else if (num == 2)
{//二進(jìn)制
for (int i = 0; i < 32; i++)
{
temp = static_cast<int>(data / pow(2.0, 31-i)); //int型,存儲(chǔ)值最大為(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)
{//十六進(jìn)制
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)
{//八進(jìn)制
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 << "的八進(jìn)制表示為: ";
myItoa(i, a, 8);
cout << a << endl;
cout << i << "的十進(jìn)制表示為: ";
myItoa(i, b, 10);
cout << b << endl;
cout << i << "的二進(jìn)制表示為: ";
myItoa(i, c, 2);
cout << c << endl;
cout << i << "的十六進(jìn)制表示為: ";
myItoa(i, d, 16);
cout << d << endl;
return 0;
}
char * itoa(int data, char*p, int num);
data是傳入的帶轉(zhuǎn)化的數(shù)字,為整型變量(data的最大值為2的31次方減去1),p是傳入的字符型指針,指向存儲(chǔ)轉(zhuǎn)換后字符串空間的首地址;num指定要轉(zhuǎn)換成幾進(jìn)制的數(shù)字字符串(二進(jìn)制,八進(jìn)制,十進(jìn)制,十六進(jìn)制)。
如有不足之處,還望指正!?。?BR>
復(fù)制代碼 代碼如下:
// TestInheritance.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#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; //標(biāo)志位 0-不存儲(chǔ) 1-存儲(chǔ)
if (num == 10)
{//十進(jìn)制
for (int i = 0; i < 10; i++)
{
temp = static_cast<int>(data / pow(10.0, 9-i));// pow(i,j),求i的j次方,temp取得當(dāng)前最高位
if (temp != 0) //去掉最前面的0
{
flag = 1;//將標(biāo)志位變?yōu)?,可以存儲(chǔ)
}
if (flag != 0)
{
*p++ = temp + '0'; //變成字符
data = data % static_cast<int>(pow(10.0, 9-i));
}
}
}
else if (num == 2)
{//二進(jìn)制
for (int i = 0; i < 32; i++)
{
temp = static_cast<int>(data / pow(2.0, 31-i)); //int型,存儲(chǔ)值最大為(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)
{//十六進(jìn)制
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)
{//八進(jìn)制
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 << "的八進(jìn)制表示為: ";
myItoa(i, a, 8);
cout << a << endl;
cout << i << "的十進(jìn)制表示為: ";
myItoa(i, b, 10);
cout << b << endl;
cout << i << "的二進(jìn)制表示為: ";
myItoa(i, c, 2);
cout << c << endl;
cout << i << "的十六進(jìn)制表示為: ";
myItoa(i, d, 16);
cout << d << endl;
return 0;
}
相關(guān)文章
C語(yǔ)言二叉樹(shù)的非遞歸遍歷實(shí)例分析
這篇文章主要介紹了C語(yǔ)言二叉樹(shù)的非遞歸遍歷,包括了先序遍歷、中序遍歷與后序遍歷,需要的朋友可以參考下2014-09-09C語(yǔ)言實(shí)現(xiàn)三子棋小游戲全程詳解
完成一個(gè)三子棋的代碼并不是很難,有困難且重要的是完成這個(gè)游戲代碼所具備的思想,因?yàn)樗枷肷系倪M(jìn)步才是真正的進(jìn)步,當(dāng)我們有了這個(gè)思想上的武器,寫(xiě)出別的代碼,難度就不會(huì)高2022-05-05C++對(duì)cin輸入字符的判斷及分段函數(shù)處理方法示例
這篇文章主要介紹了C++對(duì)cin輸入字符的判斷及分段函數(shù)處理方法,結(jié)合實(shí)例形式分析了C++輸入判斷及處理相關(guān)操作技巧,需要的朋友可以參考下2017-09-09C語(yǔ)言函數(shù)調(diào)用約定和返回值詳情
這篇文章主要介紹了C語(yǔ)言函數(shù)調(diào)用約定和返回值詳情,函數(shù)調(diào)用約定不同,會(huì)影響函數(shù)生成的符號(hào)名,函數(shù)入?yún)㈨樞?,形參?nèi)存的清理者,更多相關(guān)需要的小伙伴可以參考下文詳情介紹2022-07-07