unit Tocsg.Binary; interface uses Winapi.Windows, System.SysUtils, System.Classes; function ConvBytesToHexStr(pBuf: PByte; dwSize: DWORD): AnsiString; function ConvHexStrToBytes(sSrc: String; var pBuf: TBytes): Integer; implementation uses Tocsg.Exception; function ConvBytesToHexStr(pBuf: PByte; dwSize: DWORD): AnsiString; var i: Integer; sTemp, sHex: AnsiString; begin Result := ''; try // for i := 0 to dwSize - 1 do // Result := Result + Format('%.2x', [pBuf[i]]); // 속도 개선 25_0415 14:45:22 kku SetLength(sTemp, dwSize * 2); for i := 0 to dwSize - 1 do begin sHex := Format('%.2x', [pBuf[i]]); CopyMemory(@sTemp[(i * 2) + 1], @sHex[1], 2); end; Result := sTemp; except on E: Exception do ETgException.TraceException(E, 'Fail .. ConvBytesToHexStr()'); end; end; function ConvHexStrToBytes(sSrc: String; var pBuf: TBytes): Integer; var HexList: TStringList; i, nLen: Integer; begin nLen := Length(sSrc); if (nLen mod 2) <> 0 then begin sSrc := '0' + sSrc; Inc(nLen); end; HexList := TStringList.Create; try i := 0; while i < nLen do begin HexList.Add(sSrc.Substring(i, 2)); Inc(i, 2); end; Result := HexList.Count; if Result <= 0 then exit; SetLength(pBuf, Result); for i := 0 to HexList.Count - 1 do pBuf[i] := StrToIntDef('$' + HexList[i], 0); finally FreeAndNil(HexList); end; end; end.