---
title: "ItoA实现及测试用例"
author: "holoblog"
date: "2007-12-15T10:58:00Z"
tags: []
slug: "itoa"
draft: false
lastmod: "2011-11-11T11:23:30Z"
id: 1252035
url: "https://holoblog.iteye.com/blog/1252035"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)char\*ItoA(intvalue,char\*dest,intr)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(value&lt;0&&r!=10)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)throw"Exception";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intflag=1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(value&lt;0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)flag=-1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)value=-value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)char\*map="0123456789abcdef";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)char\*reDest=dest;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)do  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*dest++=map\[value%r];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}while(value/=r);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(flag==-1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*dest++='-';  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)\*dest='/0';  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intlen=strlen(reDest);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0,j=len-1;i&lt;j;i++,j--)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(reDest\[i],reDest\[j]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnreDest;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

测试用例：

1.10进制 正数，负数，0

2.16进制 正数

3.0进制 （Crash）

4.进制为负

5.负16进制

6.传入的char数组长度小于生成的数字长度

7.20进制

8.dest为NULL

9.value 为 2^31-1 或 -2^31