一、十进制 转换成 n 进制
这里要求 1< n <= 9
转换函数实现如下
[c-sharp] view plain copy print ?
-
- void Decimal_to_Other(int dec, char *other, int n)
- {
-
- int i=0;
- int dec_copy = dec;
- char* tmpArray = new char[64];
- while(dec_copy != 0){
- tmpArray[i] = dec_copy%n+'0';
- dec_copy /= n;
- i++;
- }
-
- int len = i;
- for(i=0;i<len;i++){
- other[i] = tmpArray[len-i-1];
- }
- other[len] = '/0';
- delete[] tmpArray;
- }
//将十进制 dec 转换成 n 进制,结果存在 other中,other在外部分配内存 void Decimal_to_Other(int dec, char *other, int n) { //1 < n <= 9 int i=0; int dec_copy = dec; char* tmpArray = new char[64]; while(dec_copy != 0){ tmpArray[i] = dec_copy%n+'0'; dec_copy /= n; i++; } //n进制的串的高位 保存在 tmpArray的地位, //如 6,二进制保存为tmpArray[0~2] = {0,1,1} int len = i; //n进制串长 for(i=0;i<len;i++){ //下面将tmpArray颠倒,存在other中 other[i] = tmpArray[len-i-1]; } //other[0~2]= {1,1,0} 这就是结果 other[len] = '/0'; delete[] tmpArray; }
调用如下:
[c-sharp] view plain copy print ?
- CString szNumber;
- char* bin = new char[64];
- Decimal_to_Other(m_Decimal,bin,2);
- szNumber.Format("%s",bin);
- AfxMessageBox(szNumber);
-
- char* octal = new char[64];
- Decimal_to_Other(m_Decimal,octal,8);
- szNumber.Format("%s",octal);
- AfxMessageBox(szNumber);
CString szNumber; char* bin = new char[64]; //转化成二进制 Decimal_to_Other(m_Decimal,bin,2); szNumber.Format("%s",bin); AfxMessageBox(szNumber); char* octal = new char[64]; //转化成八进制 Decimal_to_Other(m_Decimal,octal,8); szNumber.Format("%s",octal); AfxMessageBox(szNumber);
二、十进制 转换成 十六进制
转换函数如下
[cpp] view plain copy print ?
-
- void Decimal_to_Hex(int dec, char *hex)
- {
- int i=0,flag;
- int dec_copy = dec;
- char* tmpArray = new char[64];
- while(dec_copy != 0){
- flag = dec_copy%16;
- switch(flag){
- case 10:
- tmpArray[i] = 'A';
- break;
- case 11:
- tmpArray[i] = 'B';
- break;
- case 12:
- tmpArray[i] = 'C';
- break;
- case 13:
- tmpArray[i] = 'D';
- break;
- case 14:
- tmpArray[i] = 'E';
- break;
- case 15:
- tmpArray[i] = 'F';
- break;
- default:
- tmpArray[i] = flag+'0';
- break;
- }
- dec_copy /= 16;
- i++;
- }
- int len = i;
- for(i=0;i<len;i++){
- hex[i] = tmpArray[len-i-1];
- }
- hex[len] = '/0';
- delete[] tmpArray;
- }
//hex 在外部分配空间 void Decimal_to_Hex(int dec, char *hex) { int i=0,flag; int dec_copy = dec; char* tmpArray = new char[64]; while(dec_copy != 0){ flag = dec_copy%16; switch(flag){ case 10: tmpArray[i] = 'A'; break; case 11: tmpArray[i] = 'B'; break; case 12: tmpArray[i] = 'C'; break; case 13: tmpArray[i] = 'D'; break; case 14: tmpArray[i] = 'E'; break; case 15: tmpArray[i] = 'F'; break; default: tmpArray[i] = flag+'0'; break; } dec_copy /= 16; i++; } int len = i; //n进制串长 for(i=0;i<len;i++){ hex[i] = tmpArray[len-i-1]; } hex[len] = '/0'; delete[] tmpArray; }
调用如下:
[cpp] view plain copy print ?
- CString szNumber;
- char* hex = new char[64];
- Decimal_to_Hex(m_Decimal,hex);
- szNumber.Format("%s",hex);
- AfxMessageBox(szNumber);
CString szNumber; char* hex = new char[64]; //转化成十六进制 Decimal_to_Hex(m_Decimal,hex); szNumber.Format("%s",hex); AfxMessageBox(szNumber);
三、二进制 转换成 十进制
[c-sharp] view plain copy print ?
- int Binary_to_Decimal(CString szBin){
- int len = szBin.GetLength();
- char* str = new char[len];
- double result = 0.0;
-
- strcpy(str,szBin.GetBuffer(0));
-
- for(int i=0;i<len;i++){
- if(str[i] == '1'){
- result += pow(2,len-i-1);
- continue;
- }
- else if(str[i] == '0'){
-
- }
- else{
- AfxMessageBox("输入二进制串有误!");
- return 0;
- }
- }
- return (int)result;
- }
int Binary_to_Decimal(CString szBin){ int len = szBin.GetLength(); //二进制串长 char* str = new char[len]; //存放串 double result = 0.0; //结果 strcpy(str,szBin.GetBuffer(0)); for(int i=0;i<len;i++){ if(str[i] == '1'){ result += pow(2,len-i-1); //计算 2 的 len-i 次方,并加入到最终结果中 continue; } else if(str[i] == '0'){ } else{ AfxMessageBox("输入二进制串有误!"); return 0; } } return (int)result; }
调用如下:
[cpp] view plain copy print ?
- CString szBinary = "101101101";
- int decimal;
- CString szDecimal;
- decimal = Binary_to_Decimal(szBinary);
- szDecimal.Format("十进制 结果为 %d",decimal);
- AfxMessageBox(szDecimal);
CString szBinary = "101101101"; int decimal; CString szDecimal; decimal = Binary_to_Decimal(szBinary); szDecimal.Format("十进制 结果为 %d",decimal); AfxMessageBox(szDecimal);
四、八进制 转换成 十进制
[c-sharp] view plain copy print ?
- int Octal_to_Decimal(CString szOct){
- int len = szOct.GetLength();
- char* str = new char[len];
- double result = 0.0;
-
- int nOct;
- strcpy(str,szOct.GetBuffer(0));
-
- for(int i=0;i<len;i++){
-
- if(str[i] == '0'){
- nOct = 0;
- }
- else if(str[i] == '1'){
- nOct = 1;
- }
- else if(str[i] == '2'){
- nOct = 2;
- }
- else if(str[i] == '3'){
- nOct = 3;
- }
- else if(str[i] == '4'){
- nOct = 4;
- }
- else if(str[i] == '5'){
- nOct = 5;
- }
- else if(str[i] == '6'){
- nOct = 6;
- }
- else if(str[i] == '7'){
- nOct = 7;
- }
- else{
- AfxMessageBox("输入八进制串有误!");
- return 0;
- }
-
- result += (nOct*pow(8,len-i-1));
-
-
- }
-
- return (int)result;
- }
int Octal_to_Decimal(CString szOct){ int len = szOct.GetLength(); //串长 char* str = new char[len]; //存放串 double result = 0.0; //结果 int nOct; strcpy(str,szOct.GetBuffer(0)); for(int i=0;i<len;i++){ if(str[i] == '0'){ nOct = 0; } else if(str[i] == '1'){ nOct = 1; } else if(str[i] == '2'){ nOct = 2; } else if(str[i] == '3'){ nOct = 3; } else if(str[i] == '4'){ nOct = 4; } else if(str[i] == '5'){ nOct = 5; } else if(str[i] == '6'){ nOct = 6; } else if(str[i] == '7'){ nOct = 7; } else{ AfxMessageBox("输入八进制串有误!"); return 0; } result += (nOct*pow(8,len-i-1)); // CString msg;msg.Format("nOct = %d pow = %f result = %f",nOct,pow(8,len-i-1),result); // AfxMessageBox(msg); } // delete[] str; return (int)result; }
调用如下:
[cpp] view plain copy print ?
- CString szOctal = "1721";
- int decimal;
- CString szDecimal;
- decimal = Octal_to_Decimal(szOctal);
- szDecimal.Format("十进制 结果为 %d",decimal);
- AfxMessageBox(szDecimal);
CString szOctal = "1721"; int decimal; CString szDecimal; decimal = Octal_to_Decimal(szOctal); szDecimal.Format("十进制 结果为 %d",decimal); AfxMessageBox(szDecimal);
五、十六进制 转换成 十进制
[c-sharp] view plain copy print ?
- int Hex_to_Decimal(CString szHex){
- int len = szHex.GetLength();
- char* str = new char[len];
- double result = 0.0;
-
- int nHex;
- strcpy(str,szHex.GetBuffer(0));
-
- for(int i=0;i<len;i++){
-
- if(str[i] == '0'){
- nHex = 0;
- }
- else if(str[i] == '1'){
- nHex = 1;
- }
- else if(str[i] == '2'){
- nHex = 2;
- }
- else if(str[i] == '3'){
- nHex = 3;
- }
- else if(str[i] == '4'){
- nHex = 4;
- }
- else if(str[i] == '5'){
- nHex = 5;
- }
- else if(str[i] == '6'){
- nHex = 6;
- }
- else if(str[i] == '7'){
- nHex = 7;
- }
- else if(str[i] == '8'){
- nHex = 8;
- }
- else if(str[i] == '9'){
- nHex = 9;
- }
- else if(str[i] == 'A'){
- nHex = 10;
- }
- else if(str[i] == 'B'){
- nHex = 11;
- }
- else if(str[i] == 'C'){
- nHex = 12;
- }
- else if(str[i] == 'D'){
- nHex = 13;
- }
- else if(str[i] == 'E'){
- nHex = 14;
- }
- else if(str[i] == 'F'){
- nHex = 15;
- }
- else{
- AfxMessageBox("输入十六进制串有误!");
- return 0;
- }
-
- result += (nHex*pow(16,len-i-1));
- }
- return (int)result;
- }
int Hex_to_Decimal(CString szHex){ int len = szHex.GetLength(); //串长 char* str = new char[len]; //存放串 double result = 0.0; //结果 int nHex; strcpy(str,szHex.GetBuffer(0)); for(int i=0;i<len;i++){ if(str[i] == '0'){ nHex = 0; } else if(str[i] == '1'){ nHex = 1; } else if(str[i] == '2'){ nHex = 2; } else if(str[i] == '3'){ nHex = 3; } else if(str[i] == '4'){ nHex = 4; } else if(str[i] == '5'){ nHex = 5; } else if(str[i] == '6'){ nHex = 6; } else if(str[i] == '7'){ nHex = 7; } else if(str[i] == '8'){ nHex = 8; } else if(str[i] == '9'){ nHex = 9; } else if(str[i] == 'A'){ nHex = 10; } else if(str[i] == 'B'){ nHex = 11; } else if(str[i] == 'C'){ nHex = 12; } else if(str[i] == 'D'){ nHex = 13; } else if(str[i] == 'E'){ nHex = 14; } else if(str[i] == 'F'){ nHex = 15; } else{ AfxMessageBox("输入十六进制串有误!"); return 0; } result += (nHex*pow(16,len-i-1)); } return (int)result; }
调用如下:
[cpp] view plain copy print ?
- CString szHex = "1A2B";
- int decimal;
- CString szDecimal;
- decimal = Hex_to_Decimal(szHex);
- szDecimal.Format("十进制 结果为 %d",decimal);
- AfxMessageBox(szDecimal);
CString szHex = "1A2B"; int decimal; CString szDecimal; decimal = Hex_to_Decimal(szHex); szDecimal.Format("十进制 结果为 %d",decimal); AfxMessageBox(szDecimal);
六、利用栈实现
[cpp] view plain copy print ?
-
- void conversion(int N){
- InistStack(S);
- scanf("%d",dec);
- while(dec != 0){
- Push(S,dec%N);
- dec /= N;
- }
- while(!StackEmpty(S)){
- Pop(S,e);
- printf("%d",e);
- }
- }
PS:我实现了一个数值转化的小工具,大家可以下载使用。
http://download.csdn.net/source/2869264