不太明白你的意思,你是说你有一个4字节的字符串,它在某些时候是long型的即双重身份?
你可以用指针,得到这4字节在内存中的头地址(BYTE*)分别赋给long型指针和字符串指针。分别得到各各自的指针后想怎么用就怎么用了!
如果你只是要把long 型与字符串进行值的转化可以用sprintf和sscanf!
你可以用指针,得到这4字节在内存中的头地址(BYTE*)分别赋给long型指针和字符串指针。分别得到各各自的指针后想怎么用就怎么用了!
如果你只是要把long 型与字符串进行值的转化可以用sprintf和sscanf!
将数字转化为字符串
////Converts a floating-point number to a string.
char *_fcvt(double value/*要转换的数*/,int count/*小数点后的位数*/,int *dec/*指向存放小数点的指针*/,int *sign/*指向符号标志指针*/);
Example/* FCVT.C: This program converts the constant
* 3.1415926535 to a string and sets the pointer
* *buffer to point to that string.
*/#include <stdlib.h>
#include <stdio.h>void main( void )
{
int decimal, sign;
char *buffer;
double source = 3.1415926535; buffer = _fcvt( source, 7, &decimal, &sign );
printf( "source: %2.10f buffer: '%s' decimal: %d sign: %d\n",
source, buffer, decimal, sign );
}
Outputsource: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0 /////Converts a double number to a string.
char *_evct(double value,int count,int *dec,int *sign);Example/* ECVT.C: This program uses _ecvt to convert a
* floating-point number to a character string.
*/#include <stdlib.h>
#include <stdio.h>void main( void )
{
int decimal, sign;
char *buffer;
int precision = 10;
double source = 3.1415926535; buffer = _ecvt( source, precision, &decimal, &sign );
printf( "source: %2.10f buffer: '%s' decimal: %d sign: %d\n",
source, buffer, decimal, sign );
}
Outputsource: 3.1415926535 buffer: '3141592654' decimal: 1 sign: 0
long/int->ASCII : wsprintf/ssprintf
拜托老大你多看点书呀!
复杂言之自己动手编一个不就行了
犯不着加一个高手请进吧
偶都差点不敢进来
CString str
str.Format("%ld",temp);
//////////////////////////////////
CString IntToString(int i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;}int StringToInt(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
int i = atoi(buff);
str.ReleaseBuffer();
return i;
}CString LongToString(long i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;
}long StringToLong(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
long i = (long)atoi(buff);
str.ReleaseBuffer();
return i;
}CString DoubleToString(double d,int wei,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2,fmt,str;
s1.Format("%d",wei);
if(width == -1)
{
fmt = "%."+s1+"f";
}
else
{
s2.Format("%d",width);
fmt = "%"+s2+"."+s1+"f";
}
str.Format((LPCTSTR)fmt,d); return str;
}double StringToDouble(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
double d = atof(buff);
str.ReleaseBuffer();
return d;
}
CString DateTimeToString(COleDateTime tm,int flag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
switch(flag)
{
case 1:
return tm.Format("%Y%m%d");
break; case 2:
return tm.Format("%y%m%d");
break; case 3:
return tm.Format("%Y年%m月%d日");
break; default:
break;
}
return tm.Format("(%Y年%m月%d日)");
}/*
COleDateTime StringToTime(CString str)
{
return COleDateTime::GetCurrentTime();
}
*/
CString CurrencyToString(COleCurrency cy)
{// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Mid(s2.GetLength(),2); //得到保留的2位小数
int len = (int)s2.GetLength()/3;
if((s2.GetLength()%3)>0)
len++;
int index = s2.GetLength()-3;
for(int i=1;i<len;i++)
{
s2.Insert(index,",");
index -=3;
}
retStr = "¥"+s2+"."+s1; return retStr;}COleCurrency StringToCurrency(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2;
if(str.GetLength()>0)
{
s1 = str.SpanExcluding("-1234567890.");
str = str.Right(str.GetLength()-s1.GetLength());
s2 += str.SpanIncluding("-1234567890.");
}
return DoubleToCurrency(StringToDouble(s2)) ;}如果我写的函数有错误,请各位帮我改正,欢迎大家用我写的代码,如果你用了,最好能写个MAIL给我,好让我高兴以下。
COleCurrency DoubleToCurrency(double currency)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleCurrency cy;
long i = (long)currency ;
long j = (long)((currency - i)*10000);
if(((long)((currency - i)*100000) % 10)>4)
j++;
cy.SetCurrency(i,j);
return cy;
}double CurrencyToDouble(COleCurrency cy)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState()); CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Right(4); //得4位小数
s2 += "."+ s1;
return StringToDouble(s2);
}
//
CString IntToString(int i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;}int StringToInt(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
int i = atoi(buff);
str.ReleaseBuffer();
return i;
}CString LongToString(long i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;
}long StringToLong(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
long i = (long)atoi(buff);
str.ReleaseBuffer();
return i;
}CString DoubleToString(double d,int wei,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2,fmt,str;
s1.Format("%d",wei);
if(width == -1)
{
fmt = "%."+s1+"f";
}
else
{
s2.Format("%d",width);
fmt = "%"+s2+"."+s1+"f";
}
str.Format((LPCTSTR)fmt,d); return str;
}double StringToDouble(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
double d = atof(buff);
str.ReleaseBuffer();
return d;
}
CString DateTimeToString(COleDateTime tm,int flag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
switch(flag)
{
case 1:
return tm.Format("%Y%m%d");
break; case 2:
return tm.Format("%y%m%d");
break; case 3:
return tm.Format("%Y年%m月%d日");
break; default:
break;
}
return tm.Format("(%Y年%m月%d日)");
}/*
COleDateTime StringToTime(CString str)
{
return COleDateTime::GetCurrentTime();
}
*/
CString CurrencyToString(COleCurrency cy)
{// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Mid(s2.GetLength(),2); //得到保留的2位小数
int len = (int)s2.GetLength()/3;
if((s2.GetLength()%3)>0)
len++;
int index = s2.GetLength()-3;
for(int i=1;i<len;i++)
{
s2.Insert(index,",");
index -=3;
}
retStr = "¥"+s2+"."+s1; return retStr;}COleCurrency StringToCurrency(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2;
if(str.GetLength()>0)
{
s1 = str.SpanExcluding("-1234567890.");
str = str.Right(str.GetLength()-s1.GetLength());
s2 += str.SpanIncluding("-1234567890.");
}
return DoubleToCurrency(StringToDouble(s2)) ;}
COleCurrency DoubleToCurrency(double currency)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleCurrency cy;
long i = (long)currency ;
long j = (long)((currency - i)*10000);
if(((long)((currency - i)*100000) % 10)>4)
j++;
cy.SetCurrency(i,j);
return cy;
}double CurrencyToDouble(COleCurrency cy)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState()); CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Right(4); //得4位小数
s2 += "."+ s1;
return StringToDouble(s2);
}
//
CString IntToString(int i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;}int StringToInt(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
int i = atoi(buff);
str.ReleaseBuffer();
return i;
}CString LongToString(long i,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString fmt,str;
if(width == -1)
{
str.Format("%d",i);
}
else
{
fmt.Format("%d",width);
fmt = "%"+fmt+"d";
str.Format((LPCTSTR)fmt,i);
} return str;
}long StringToLong(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
long i = (long)atoi(buff);
str.ReleaseBuffer();
return i;
}CString DoubleToString(double d,int wei,int width)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2,fmt,str;
s1.Format("%d",wei);
if(width == -1)
{
fmt = "%."+s1+"f";
}
else
{
s2.Format("%d",width);
fmt = "%"+s2+"."+s1+"f";
}
str.Format((LPCTSTR)fmt,d); return str;
}double StringToDouble(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
char *buff = str.GetBuffer(str.GetLength());
double d = atof(buff);
str.ReleaseBuffer();
return d;
}
CString DateTimeToString(COleDateTime tm,int flag)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
switch(flag)
{
case 1:
return tm.Format("%Y%m%d");
break; case 2:
return tm.Format("%y%m%d");
break; case 3:
return tm.Format("%Y年%m月%d日");
break; default:
break;
}
return tm.Format("(%Y年%m月%d日)");
}/*
COleDateTime StringToTime(CString str)
{
return COleDateTime::GetCurrentTime();
}
*/
CString CurrencyToString(COleCurrency cy)
{// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Mid(s2.GetLength(),2); //得到保留的2位小数
int len = (int)s2.GetLength()/3;
if((s2.GetLength()%3)>0)
len++;
int index = s2.GetLength()-3;
for(int i=1;i<len;i++)
{
s2.Insert(index,",");
index -=3;
}
retStr = "¥"+s2+"."+s1; return retStr;}COleCurrency StringToCurrency(CString str)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString s1,s2;
if(str.GetLength()>0)
{
s1 = str.SpanExcluding("-1234567890.");
str = str.Right(str.GetLength()-s1.GetLength());
s2 += str.SpanIncluding("-1234567890.");
}
return DoubleToCurrency(StringToDouble(s2)) ;}
COleCurrency DoubleToCurrency(double currency)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
COleCurrency cy;
long i = (long)currency ;
long j = (long)((currency - i)*10000);
if(((long)((currency - i)*100000) % 10)>4)
j++;
cy.SetCurrency(i,j);
return cy;
}double CurrencyToDouble(COleCurrency cy)
{
// AFX_MANAGE_STATE(AfxGetStaticModuleState()); CURRENCY cy1 = cy.m_cur;
CString s1,s2,retStr;
s1.Format("%I64d",cy1.int64); //得到包含4位小数的长整数串
s2 = s1.Left(s1.GetLength()-4); //得到整数部分
s1 = s1.Right(4); //得4位小数
s2 += "."+ s1;
return StringToDouble(s2);
}