C++中int与char*、int与Byte、double与char*的相互装换
来源:互联网 发布:seo与sem有和区别 编辑:程序博客网 时间:2024/05/21 19:45
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">郑重说明:本文绝对原创!!!</span>
在我做课题的时候发现,在C#中int与char*、int与Byte、double与char*的相互装换,都可以采用系统封装好的函数。
但是,不是我想要的结果,和自己调用,不是很适合我自己,于是自己用C++写了函数,生成dll文件,在C#中调用。
这里主要讲C++语言实现int与char*、int与Byte、double与char*的相互装换!
vs2010中可以用try...catch throw 抛出异常!或者std::overflow_error的异常 std::runtime_error
1、int与char*相互转换
char*即可以理解为字符串string类的基础
(1)IntToChar函数
int转换后的结果放在char *b中 char *b的大小在main()函数中定义大小,如char *b=new char[32]; //int 32位 大小可以自己设定。
计算char *的长度,不调用#include<string.h> ,VS2010中为#include<string>
int mystrlen(char *str)
{
int len=0;
while(*str)
{
str++;
len++;
}
return len; //返回char *长度
}
char *IntToChar(int a){//实现char *itoa(int value,char *str,int radix),radix是转换的进制 2、8、10、16char *b=new char[32]; //int 32位int i=0;int flag=1;if(a<0){b[i++]='-';a=0-a;flag=-1;}while(a){b[i++]=a%10+'0';a/=10;}b[i]='\0';//int n=mystrlen(b);int n=strlen(b);char c;int j=0;if(flag==-1){j=1;}int k=0; //控制负数for(;j<n/2;j++,k++){c=b[j];b[j]=b[n-k-1];b[n-k-1]=c;}return b;}
当然C中int2str代码比较简单,代码如下:
void int2str(char *s,int a)
{
sprintf(s,"%d",a);
}
(2)CharToInt函数
char *转int比较简单,代码如下
int CharToInt(char *b){//实现int atoi(const char *str)if(b==NULL) //如果为空return 0;int a=0;char *t=b;int flag=1;while(*t==' ') //跳过空格{t++;}if(*t=='-') //如果为负数{flag=-1;t++;}if(*t=='+'){flag=1;t++;}while((*t!='\0') && ((*t>='0') && (*t<='9'))){a*=10;a+=(*t++) - '0'; //a = *t++ : a = *t;t++;指针指向的值不变,指针自加, a = (*t)++ : 指针不变,指针指向的值+1}a*=flag;return a;}
2 int与Byte的相互转换
(1)IntToByte函数
int转Byte,Byte的结果存放在unsigned char *b中, cout输出用cout<<hex<<b[i]<<" "。不然输出结果不正确。
void IntToByte(int a,unsigned char *b)
{
//低字节到高字节,低位到高位 int为4字节,*b大小应该为4。
for(int i=0;i<4;i++)
b[i]=a>>(8*i);
}
(2)ByteToInt函数
这个函数比较简单,重点是需要有一个temp变量
int ByteToInt(unsigned char *b)
{
int a=0,temp=0;
for(int i=3;i>=0;i--) //先高位,再低位
{
temp=0;
temp |= b[i];
a+=(temp<<(8*i));
}
return a;
}
3 double与char *的相互转换
(1)DoubleToChar函数
思想是想把double的整数部分处理,然后再处理小数部分。当然整数处理部分可以调用之前的IntToChar函数
char *DoubleToChar(double a){char *c=new char[64]; //double 64位int b=(int)a;int i=0;int flag=1;char z; //处理整数部分if(b!=0){if(b<0) { c[i++]='-'; b=0-b; flag=-1; } while(b) { c[i++]=b%10+'0'; b/=10; }int j=0;if(flag==-1) { j=1; } int k=0; //控制负数for(;j<i/2;j++,k++){z=c[j];c[j]=c[i-k-1];c[i-k-1]=z;}}elsec[i++]='0';c[i++]='.'; //处理小数部分 8位小数if(flag==-1){a=0-a;}double d=a-(int)a;int t=0;int n=0;if(d!=0.0){ while((d<1)&&(n<8)) //小数位数留8位 { d *= 10;t=int(d);c[i++]=t+'0';d=d-t;n++; } c[i++]=d;}else if(0.0==d)c[i++]='0';c[i]='\0';return c;}
(2)CharToDouble函数
double CharToDouble(char *c){if(c==NULL)return 0;double r=0; char *t=c;int flag=1; //记录正负while(*t==' ') //跳过空格{t++;}if(*t=='-') //如果为负数{flag=-1;t++;}if(*t=='+'){flag=1;t++;}while(*t!='.') //处理整数部分{if((*t>='0') && (*t<='9')){r *= 10; r += (*t++)-'0';}elsereturn 0;}*t++; //跳过'.' 处理小数部分int i=0,a=0;while(*t!='\0'){if((*t>='0') && (*t<='9')){a *= 10; a += (*t++)-'0'; i++; //记录小数位数}elsereturn 0;}double n=1;for(int j=0;j<i;j++)n*=10.0;double d= a/n;r+=d;r*=flag;return r;}调用,输出为:cout<<setprecision(16)<<CharToDouble(b)<<endl; //#include<iomanip>
不然会被四舍五入输出!
- C++中int与char*、int与Byte、double与char*的相互装换
- int、char、double与byte相互转换
- int、char、double与byte相互转换
- int、char、double与byte相互转换
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、与byte相互转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- VC 中 Cstring 与 int、float、double、char、string数据类型的相互转换
- int 与 char 相互转换
- c++中int double 与char,string之间的转换
- 第一次只出现一次的字符
- USACO 1.1 Friday the Thirteenth
- Netsh命令详解
- Spring学习笔记(八):数据库连接池的小例子
- HDU 1711 Number Sequence(kmp)
- C++中int与char*、int与Byte、double与char*的相互装换
- 数据结构入门--笔记
- oracle用户密码过期的处理方法
- 读书笔记之TCP/IP1(2)
- Spring学习笔记(九):Spring拦截器和java过滤器的区别
- Servlet完全教程
- opengl 学习笔记(3) 2D 点、线、坐标、键盘
- Web第三周-Web前端利用Js实现简单计算器功能
- CSS选择器的浏览器支持