Search

'HexToStr'에 해당되는 글 1건

  1. 2009.05.01 Delphi - StrToBin, StringToHex, HexToString

Delphi - StrToBin, StringToHex, HexToString

Programming/Delphi 2009.05.01 06:09 Posted by 파란크리스마스

출처 : http://www.xxlinux.com/linux/dev/Delphi/2007-10-30/11392.html
http://dolba.net/old_k2club/bbs/bbs.php3?board=delphi&mode=view&id=345&page=32&recnum=359&keyword=&flag=
http://www.pic16.com/BBS/dispbbs.asp?boardID=31&ID=17357&page=3

function StrToBin(const S: string): string;
const
  BitArray: array[0..15] of string =
      ('0000', '0001', '0010', '0011',
       '0100', '0101', '0110', '0111',
       '1000', '1001', '1010', '1011',
       '1100', '1101', '1110', '1111');
var
  Index: Integer;
  LoBits: Byte;
  HiBits: Byte;
begin
  Result := '';
  for Index := 1 to Length(S) do
  begin
      HiBits := (Byte( S[Index]) and $F0) shr 4;
      LoBits := Byte( S[Index]) and $0F;
      Result := Result + BitArray[HiBits];
      Result := Result + BitArray[LoBits];
  end;
end;

function StringToHex(const S: string): string;
var
  Index: Integer;
begin
  Result := '';
  for Index := 1 to Length(S) do
    Result := Result + IntToHex( Byte( S[Index] ), 2 );
end;

function TransChar(AChar: Char): Integer;
begin
  if AChar in ['0'..'9'] then
  Result := Ord(AChar) - Ord('0')
  else
  Result := 10 + Ord(AChar) - Ord('A');
end;

function HexToString(aHex: String): String;
var
  I : Integer;
  CharValue: Word;
begin
  Result := '';
  for I := 1 to Trunc(Length(aHex)/2) do begin
    Result := Result + ' ';
    CharValue := TransChar(aHex[2*I-1])*16 + TransChar(aHex[2*I]);
    Result[I] := Char(CharValue);
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(
    '한글'' Hex Code is ' + StringToHex('한글') + chr(13) +
    '한글'' Binary Code is ' + StrToBin('한글') + chr(13) +
    '한글'' HexToString ' + HexToString(StringToHex('한글'))
  );
end;

신고


 

티스토리 툴바