2009년 03월 09일
문자열 변환 (HexToDec, DecToHex)
[C++ 버전]
void DecToHex(unsigned char* out_buff, int* out_buff_len, unsigned char* in_buff, int in_buff_len)
{
char hex_str[3];
memset(out_buff, 0, *out_buff_len);
for (int i = 0; i < in_buff_len; i++) {
sprintf(hex_str, "%02X", in_buff[i]);
strcat((char *)out_buff, hex_str);
}
}
void HexToDec(unsigned char* out_buff, int* out_buff_len, unsigned char* in_buff, int in_buff_len)
{
int num, i, j;
unsigned char hex_str[3];
char* stop_str;
hex_str[2] = '\0';
memset(out_buff, 0, *out_buff_len);
for (i = 0, j = 0; i < in_buff_len; i++, j++) {
hex_str[0] = in_buff[i];
hex_str[1] = in_buff[++i];
num = strtol((char *)hex_str, &stop_str, 16);
out_buff[j] = num;
}
*out_buff_len = j;
}
[Delphi 버전]
function DecToHex(sDec : String) : String;
var
i : Integer;
begin
sHex := '';
for i := 0 to StrLen(sDec) do begin
sHex := sHex + Format('%02X', [sDecimal[i]]);
end;
Result := sHex
end;
function HexToDec(sHex : String) : String;
var
i : Integer;
sDecimal : String;
begin
sDecimal := '';
for i := 1 to StrLen(PChar(sHex)) do begin
sDecimal := sDecimal + IntToStr(StrToInt('$' + sHex[i] + Temp[i+1])));
i := i + 2;
end;
Result := sDecimal
end;
# by | 2009/03/09 18:58 | Visual C++ | 트랙백 | 덧글(0)



