深入C++實(shí)現(xiàn)函數(shù)itoa()的分析
更新時(shí)間:2013年05月29日 11:43:33 作者:
本篇文章是對C++實(shí)現(xiàn)函數(shù)itoa()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
函數(shù)itoa()是將整數(shù)型轉(zhuǎn)換為c語言風(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)制)。
如有不足之處,還望指正?。?!
// TestInheritance.cpp : 定義控制臺應(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)制)。
如有不足之處,還望指正?。?!
復(fù)制代碼 代碼如下:
// TestInheritance.cpp : 定義控制臺應(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)文章
Qt重寫QComboBox實(shí)現(xiàn)下拉展示多列數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Qt如何重寫QComboBox實(shí)現(xiàn)下拉展示多列數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
C++中使用vector存儲(chǔ)并遍歷數(shù)據(jù)的基本步驟
C++標(biāo)準(zhǔn)模板庫(STL)提供了多種容器類型,包括順序容器、關(guān)聯(lián)容器、無序關(guān)聯(lián)容器和容器適配器,每種容器都有其特定的用途和特性,這篇文章主要介紹了C++中使用vector存儲(chǔ)并遍歷數(shù)據(jù)的基本步驟,需要的朋友可以參考下2025-01-01
Qt 使用 canon edsdk 實(shí)現(xiàn)實(shí)時(shí)預(yù)覽的示例代碼
這篇文章主要介紹了Qt 使用 canon edsdk 實(shí)現(xiàn)實(shí)時(shí)預(yù)覽的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
C語言中atoi函數(shù)模擬實(shí)現(xiàn)詳析
atoi函數(shù)功能是將數(shù)字字符串轉(zhuǎn)換為整數(shù),比如數(shù)字字符串"12345"被atoi轉(zhuǎn)換為12345,數(shù)字字符串"-12345"被轉(zhuǎn)換為-12345,下面這篇文章主要給大家介紹了關(guān)于C語言中atoi函數(shù)模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2022-10-10
C語言關(guān)于注釋的知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于C語言關(guān)于注釋的知識點(diǎn)總結(jié),需要的朋友們可以參考學(xué)習(xí)下。2020-02-02

