|
일단 간단히 한 byte를 Hex String을 바꾸려면 IntToHex( ch ,2 ) 이런식으로 변환하면 됩니다.
//직접 만들면 이런식이죠...
String __fastcall IntToHex2(unsigned char ch)
{
String sRslt=" ";
unsigned char tmp=ch>>4;
if(tmp<10)sRslt[1]=tmp+0x30;
else sRslt[1]=tmp+0x37;
tmp=ch&0x0F;
if(tmp<10)sRslt[2]=tmp+0x30;
else sRslt[2]=tmp+0x37;
return sRslt;
}
//---------------------------------------------------------------------------
//직접 만들필요까진 없고.. 그냥 VCL에 잘 만들어진 함수를 쓰시면 되죠\
// 아래는 IntToHex를 이용해 StrToHexString 함수를 만들어 봤습니다.
//---------------------------------------------------------------------------
String __fastcall StrToHexString( String str)
{
String sRslt="";
int ilen=str.Length();
for( int i=1; i<=ilen;i++)
{
sRslt = sRslt + IntToHex(str[i],2)+" ";
}
return sRslt;
}
//---------------------------------------------------------------------------
String __fastcall StrToHexString( char * str)
{
String sRslt="";
int ilen=strlen(str);
for( int i=0; i<ilen;i++)
{
// sRslt = sRslt + IntToHex(str[i],2)+" ";
sRslt = sRslt + IntToHex2(str[i])+" ";
}
return sRslt;
}
//---------------------------------------------------------------------------
String __fastcall DataToHexString(unsigned char *buf , int size)
{
String sRslt="";
for( int i=0; i<size;i++)
{
sRslt = sRslt + IntToHex(*buf,2)+" ";
buf++;
}
return sRslt;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage(StrToHexString("13412341324,adfalsADFADF"));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
unsigned char buf[5]={0x11,0x12,0x23,0x45,0x34};
ShowMessage(DataToHexString(buf,sizeof(buf)));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
String ss="adlfjaldjfaldjflasd\r\n";
ShowMessage(StrToHexString(ss));
}
//---------------------------------------------------------------------------
그럼....
이한진 님이 쓰신 글 :
: 스트링 문자를 아스키 값으로 출력하는 방법이 없나요..
:
: 실은 cport를 이용하여 통신하는 프로그램을 작성하였는데
:
: 스트링 문자로 데이타를 주소받는것은 별 문제가 없습니다.
: 에디터에 "1234'라는 문자를 입력하여 전송하면
: 반대편 컴퓨터에서도 "1234"가 출력되거든요..
:
: 하지만 이것과 더불어
: "1234"를 전송하면
: 받는측에서는 메모창에 31 32 33 34 0d 0a를 출력할 수 있게끔 하고 싶거든요..
: 뒤에 0d 0a 는 cr+lf이구요..
:
: 여러가지 해봤는데..잘 않되네요..ㅜㅜ
:
: 고수분들의 답변 부탁드립니다.
|