进制转换
2014-08-27 23:14
369 查看
#include <iostream> using namespace std; //十进制转16进制 char tohex(int num) { if(num>=10&&num<=15) { return 'A'+num-10; } return num+'0'; } void func(int input,char *buf) { int num,n=0; char temp; while(input) { num=input%16; buf[n++]=tohex(num); input/=16; } buf ='\0'; for(int i=0,j=n-1;i<j;i++,j--) { temp=buf[i]; buf[i]=buf[j]; buf[j]=temp; } } //16进制转10进制 int toNum(char hex) { int n=0; if(hex>='a'&&hex<='f') { n=hex-'a'+10; } else if(hex>='A'&&hex<='F') { n=hex-'A'+10; } else n=hex-'0'; return n; } int func2(char *input) { int num,result=0; while(*input!='\0') { num=toNum(*input); result=result*16+num; input++; } return result; } int main() { int a=58; //char *buf=new char[5]; //func(a,buf); //cout<<buf; cout<<func2("3a"); return 0; }
相关文章推荐
- ACM 4188. 进制转换
- 进制转换
- 【ACM-Steps1.2】简单字符串处理、简单数学题、进制转换系列8题(一)
- EXCEL用函数将二进制数转为八、十、十六进制,进制转换
- C++ 进制转换(HDUOJ 2031)
- day04,数组的操作,遍历,查找,选择冒泡排序,进制转换,二维数组
- 进制转换
- 栈——进制转换
- ACM2031_进制转换(使用了递归,代码超少的啦!!)
- 题目1138:进制转换
- 2013年8月16日、整理数组|常见功能查找|折半查找|查表法|进制转换
- Java 进制转换、位运算、逻辑运算
- C# 进制转换
- 进制转换
- 数的进制转换
- Java变量与进制转换
- C++第17周项目4 数组用于进制转换
- 2031 进制转换
- 进制转换
- OJ_1080 进制转换