c++常用函数之 十六进制字符串转换为十进制
2013-06-26 16:19
375 查看
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return 2;//超出范围
}
char* pc = new char[str.length()];
if (pc==NULL)
{
return -2;//内存申请失败
}
memset(pc,0,str.length());
str.copy(pc,2);
string strTmp(pc);
int cpyCount=0;
memset(pc,0,str.length());
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
cpyCount = str.copy(pc,str.size()-2,2);
}else{
cpyCount = str.copy(pc,str.size());
}
n=0;
char *ptmp = pc;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
n += (*pc-'0')*static_cast<int>(pow(16,cpyCount-i-1));
}else if (*pc>='A' && *pc<='F'){
n += (*pc-'A'+10)*static_cast<int>(pow(16,cpyCount-i-1));
}else if (*pc>='a' && *pc<='f'){
n += (*pc-'a'+10)*static_cast<int>(pow(16,cpyCount-i-1));
}else{
return -1;//含有非法字符
}
pc++;
}
pc = ptmp;
delete []pc;
pc=NULL;
return 0;//正常退出
}
第二中实现方法
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return 2;//超出范围
}
char* pc = new char[str.length()];
if (pc==NULL)
{
return -2;//内存申请失败
}
memset(pc,0,str.length());
str.copy(pc,2);
string strTmp(pc);
int cpyCount=0;
memset(pc,0,str.length());
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
cpyCount = str.copy(pc,str.size()-2,2);
}else{
cpyCount = str.copy(pc,str.size());
}
n=0;
char *ptmp = pc;
int mid;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
mid = *pc-'0';
}else if (*pc>='A' && *pc<='F'){
mid = *pc-'A'+10;
}else if (*pc>='a' && *pc<='f'){
mid = *pc-'a'+10;
}else{
return -1;//含有非法字符
}
mid <<= ((cpyCount-i-1)<<2);//用移位方式实现 ((cpyCount-i-1)<<2)这样做的目的就是因为二进制每四位组成十六进制的一个位
n += mid;
pc++;
}
pc = ptmp;
delete []pc;
pc=NULL;
return 0;//正常退出
}
if (str.length()>8)
{
return 2;//超出范围
}
第三种做法
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return -1;//超出范围
}
string strTmp;
strTmp = str.substr(0,2);
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
strTmp = str.substr(2,str.length()-2);
}else{
strTmp = str;
}
n=0;
char *pc = const_cast<char*>(strTmp.c_str());
int cpyCount=strTmp.length();
int mid;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
mid = *pc-'0';
}else if (*pc>='A' && *pc<='F'){
mid = *pc-'A'+10;
}else if (*pc>='a' && *pc<='f'){
mid = *pc-'a'+10;
}else{
return -1;//含有非法字符
}
mid <<= ((cpyCount-i-1)<<2);
n += mid;
pc++;
}
return 0;//正常退出
}
#include <math.h>
#include <string>
using namespace std;
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return 2;//超出范围
}
char* pc = new char[str.length()];
if (pc==NULL)
{
return -2;//内存申请失败
}
memset(pc,0,str.length());
str.copy(pc,2);
string strTmp(pc);
int cpyCount=0;
memset(pc,0,str.length());
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
cpyCount = str.copy(pc,str.size()-2,2);
}else{
cpyCount = str.copy(pc,str.size());
}
n=0;
char *ptmp = pc;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
n += (*pc-'0')*static_cast<int>(pow(16,cpyCount-i-1));
}else if (*pc>='A' && *pc<='F'){
n += (*pc-'A'+10)*static_cast<int>(pow(16,cpyCount-i-1));
}else if (*pc>='a' && *pc<='f'){
n += (*pc-'a'+10)*static_cast<int>(pow(16,cpyCount-i-1));
}else{
return -1;//含有非法字符
}
pc++;
}
pc = ptmp;
delete []pc;
pc=NULL;
return 0;//正常退出
}
第二中实现方法
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return 2;//超出范围
}
char* pc = new char[str.length()];
if (pc==NULL)
{
return -2;//内存申请失败
}
memset(pc,0,str.length());
str.copy(pc,2);
string strTmp(pc);
int cpyCount=0;
memset(pc,0,str.length());
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
cpyCount = str.copy(pc,str.size()-2,2);
}else{
cpyCount = str.copy(pc,str.size());
}
n=0;
char *ptmp = pc;
int mid;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
mid = *pc-'0';
}else if (*pc>='A' && *pc<='F'){
mid = *pc-'A'+10;
}else if (*pc>='a' && *pc<='f'){
mid = *pc-'a'+10;
}else{
return -1;//含有非法字符
}
mid <<= ((cpyCount-i-1)<<2);//用移位方式实现 ((cpyCount-i-1)<<2)这样做的目的就是因为二进制每四位组成十六进制的一个位
n += mid;
pc++;
}
pc = ptmp;
delete []pc;
pc=NULL;
return 0;//正常退出
}
if (str.length()>8)
{
return 2;//超出范围
}
第三种做法
int HexToDec(const string str,int& n){
if (str.empty())
{
return 1;//字符串为空
}
if (str.length()>8)
{
return -1;//超出范围
}
string strTmp;
strTmp = str.substr(0,2);
if (strTmp.compare("0x")==0 || strTmp.compare("0X")==0)
{
strTmp = str.substr(2,str.length()-2);
}else{
strTmp = str;
}
n=0;
char *pc = const_cast<char*>(strTmp.c_str());
int cpyCount=strTmp.length();
int mid;
for (int i=0;i<cpyCount;i++)
{
if (*pc>='0' && *pc<='9'){
mid = *pc-'0';
}else if (*pc>='A' && *pc<='F'){
mid = *pc-'A'+10;
}else if (*pc>='a' && *pc<='f'){
mid = *pc-'a'+10;
}else{
return -1;//含有非法字符
}
mid <<= ((cpyCount-i-1)<<2);
n += mid;
pc++;
}
return 0;//正常退出
}
相关文章推荐
- C++实现十六进制字符串转换为十进制整数的方法
- 将一个十六进制字符串转换为十进制数值的问题|| 以及C++中string转换成char*|| c++文件按行输入
- 一个把十六进制字符串转换为十进制数字的函数
- c++两个常用函数:1.二进制字符串2 hex(十六进制)。2. 栈变量字符串2堆变量
- c++两个常用函数:1.二进制字符串2 hex(十六进制)。2. 栈变量字符串2堆变量
- IOS中常用的字符串、十进制、二进制、十六进制之间互相转换及简单算术和
- 在SQL中如何将十进制的值转换为十六进制的字符串显示出来?
- mssql中十进制转换成十六进制字符串
- 将一个十六进制字符串转换为十进制数值的问题
- python常用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)
- C语言将字符串转换成对应的数字(十进制、十六进制)
- C++将十六进制转换为十进制(包括小a的情况)
- 练习 2-3 编写函数 htoi(s),把由十六进制数字组成的字符串(包含可选的前缀 0x 或 0X)转换为与之等价的整型值。字符串中允许包含的数字包括:0~9、a~f 以及 A~F。
- C/C++字符串转换到数字的函数
- 字符串与整数、浮点数、无符号整数之间的转换常用函数
- C/C++ —— 十六进制类型字符串的转换
- Android字节、十进制、十六进制、字符串之间的相互转换
- 字符串转换为NSdata数据/十进制转换为十六进制
- VC字符串转换常用函数
- Android开发中遇到的字符串、十六进制和Byte转换,及十六进制校验等函数集