如何将字符串转换为十六进制的字符?
如何将"123"(ansistring)转换为十六进制的两位字符?谢谢 问题点数:20、回复次数:11Top
1 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2002-02-21 17:14:01 得分 0
String s="123";
int i=s.ToIntDef(0);
char buf[16];
sprintf(buf,"%02x",i); // #include "stdio.h"
ShowMessage(buf);
Top
2 楼camel20(william)回复于 2002-02-21 17:22:31 得分 0
同意楼上的!Top
3 楼invalid(空心菜(Python是个好东东,大家多用用!))回复于 2002-02-21 17:42:46 得分 0
先把字符串转换成int型,然后用下面的任意一种方法把int型转换成字符串。
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
int nValue = 378;
Label1->Caption = IntToHex(nValue,8); //0000017A
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
int nValue = 452;
char buf[40];
itoa(nValue, buf, 16); // 2 means binary, 16 would be hex
Label1->Caption = buf;//1c4
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
// using streams to display a number in hex.
int nValue = 452;
ostringstream ostr;
ostr << hex << nValue;
Label1->Caption = ostr.str().c_str();//1c4
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn4Click(TObject *Sender)
{
// using sprintf to display a number in hex.
int nValue = 452;
char buf[40];
sprintf(buf, "%08X", nValue);
Label1->Caption = buf;//000001C4
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn5Click(TObject *Sender)
{
// using AnsiString::sprintf to display a number in hex.
int nValue = 452;
AnsiString str;
str.sprintf("%08x", nValue);
Label1->Caption = str;//00001c4
}
Top
4 楼wsf()回复于 2002-02-22 13:25:56 得分 0
这个问题我已经完成拉,我想知道如何作类型强制转换,类似把结构型转换为字符串型.谢谢Top
5 楼kingcaiyao(aking)回复于 2002-02-22 13:49:21 得分 0
please see the following code:
we assume that one variable named caiyao and its value is '9888' or 'ABCDF'
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int caiyao=9888;
String S=String(IntToHex(caiyao,4));
MessageBox(NULL,S.c_str(),"how are you",MB_OK);
}
Top
6 楼wsf()回复于 2002-02-22 15:26:58 得分 0
转换我已经知道拉,我现在遇到一个新的问题:比如
struct TTemp{
char a[10];
char b;
}
如何将此结构的变量值转换为str(ansistring)?Top
7 楼lyl_rabbit(阿牛)回复于 2002-02-22 15:50:05 得分 0
关注Top
8 楼zhangchao_7622(zhangchao_7622)回复于 2002-02-22 18:10:30 得分 0
关注Top
9 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2002-02-22 20:02:10 得分 20
struct TTemp x;
String s=String((char *)&x);
Top
10 楼BCB(天下三分明月夜,二分无赖是扬州)回复于 2002-02-22 20:03:44 得分 0
要强制转换?!
struct TTemp x;
AnsiString s=(AnsiString &)x;
Top
11 楼wsf()回复于 2002-02-22 21:49:31 得分 0
to BCB谢谢,能留下您的email,今后多交流好吗?Top




