Board index » delphi » How to convert from ascii to Hex (and vice versa)

How to convert from ascii to Hex (and vice versa)

I'm converting some of my VB tools to Delphi.  I have a testing tool
written in VB that converts text to its Hex value, and/or Hex values to
ascii.  This is so easy to do in VB (and even easier in C), that I must
be missing something obvious.  

I've scoured the Help files and the docs, but haven't figured out how to
do this.  I can use IntToHex to turn an integer val to Hex, but how do I
convert the characters to Int falues?    StrToInt wants a string of
numbers (i.e.  '12345').

Any ideas?

************************************************************
* pa...@ix.netcom.com   software eng/dev/test/hack         *
* NetCrusher...Someone had to make the  AOL'rs look good!  *
************************************************************

 

Re:How to convert from ascii to Hex (and vice versa)


In article <41r5m7$2po_...@ix.netcom.com>, pa...@ix.netcom.com says...

Quote

>I'm converting some of my VB tools to Delphi.  I have a testing tool
>written in VB that converts text to its Hex value, and/or Hex values to
>ascii.  This is so easy to do in VB (and even easier in C), that I must
>be missing something obvious.  

>I've scoured the Help files and the docs, but haven't figured out how to
>do this.  I can use IntToHex to turn an integer val to Hex, but how do I
>convert the characters to Int falues?    StrToInt wants a string of
>numbers (i.e.  '12345').

>Any ideas?

Try  Ord() which is the logical equivalent of ASC() or ASCII().

  if ( Ord('A') = 65) then IFoundIt := TRUE;

Hope this helps
_____________________________________________________________________________
Kirk Wolak                                   It's not whether you win or lose
KWo...@mail.cbf.com                          It's how you place the blame!
//
// Views expressed are my own and not those of the company I work for!
//

Re:How to convert from ascii to Hex (and vice versa)


Quote
Dalan (pa...@ix.netcom.com) wrote:

: I'm converting some of my VB tools to Delphi.  I have a testing tool
: written in VB that converts text to its Hex value, and/or Hex values to
: ascii.  This is so easy to do in VB (and even easier in C), that I must
: be missing something obvious.  
:
: I've scoured the Help files and the docs, but haven't figured out how to
: do this.  I can use IntToHex to turn an integer val to Hex, but how do I
: convert the characters to Int falues?    StrToInt wants a string of
: numbers (i.e.  '12345').
:
: Any ideas?
:
: ************************************************************
: * pa...@ix.netcom.com   software eng/dev/test/hack         *
: * NetCrusher...Someone had to make the  AOL'rs look good!  *
: ************************************************************
put a $ sign in front of the characters and itt interepts it as HEX string

ie '$12345' will return a int value of 74565 .

Re:How to convert from ascii to Hex (and vice versa)


In message <41r5m7$2po_...@ix.netcom.com>
        pa...@ix.netcom.com (Dalan) writes:

Quote
> I'm converting some of my VB tools to Delphi.  I have a testing tool
> written in VB that converts text to its Hex value, and/or Hex values to
> ascii.  This is so easy to do in VB (and even easier in C), that I must
> be missing something obvious.  
> I've scoured the Help files and the docs, but haven't figured out how to
> do this.  I can use IntToHex to turn an integer val to Hex, but how do I
> convert the characters to Int falues?    StrToInt wants a string of
> numbers (i.e.  '12345').
> Any ideas?

Use StrToInt, but just add a dollar sign to the beginning of the hex
string you want to convert, eg...

procedure TForm1.Button1Click(Sender: TObject);
Var i : integer;
    s : string;
begin
     s:='1FA';
     i:=StrToInt('$' + s);
     Label1.caption:=IntToStr(i);
end;

Regards... Roland

Re:How to convert from ascii to Hex (and vice versa)


Quote
In article <41r5m7$2po_...@ix.netcom.com> pa...@ix.netcom.com (Dalan) writes:
>From: pa...@ix.netcom.com (Dalan)
>Subject: How to convert from ascii to Hex (and vice versa)
>Date: 28 Aug 1995 04:25:51 GMT
>I've scoured the Help files and the docs, but haven't figured out how to
>do this.  I can use IntToHex to turn an integer val to Hex, but how do I
>convert the characters to Int falues?    StrToInt wants a string of
>numbers (i.e.  '12345').
>Any ideas?

I'm sure one of the Format procedures will do it if you give it the right
format specifier string. Try searching the help for format or format specifiers

Re:How to convert from ascii to Hex (and vice versa)


Quote
In article <41tki1$...@status.gen.nz> mich...@iconz.co.nz (Michael Stevenson) writes:
>put a $ sign in front of the characters and itt interepts it as HEX string
>ie '$12345' will return a int value of 74565 .

Gee, that's cute...  (he said, highlighting a procedure in his current program
and pressing the 'Del' key...)  

/mr/

Re:How to convert from ascii to Hex (and vice versa)


Quote
>>I'm converting some of my VB tools to Delphi.  I have a testing tool
>>written in VB that converts text to its Hex value, and/or Hex values
to
>>ascii.  This is so easy to do in VB (and even easier in C), that I
must
>>be missing something obvious.  

>Try  Ord() which is the logical equivalent of ASC() or ASCII().

>  if ( Ord('A') = 65) then IFoundIt := TRUE;

>Hope this helps
>_______________________________________________________________________

I'm still stuck.  I had tried Ord, but I assume that I've missed a step.
For now, imagine a simplified version of this app that contains an edit
box, a memo field, and a command button; and that I want to enter 1 char
into the edit box, then display its hex value in the memo field.  

var
  sOrigVal  :  String[1];
begin
   sOrigVal := edit1.text;
   iOrd     := Ord(sOrigVal); {This fail because Ord
                               won't take a string var}
   Memo1.Text := 'The Hex Value is ' + IntToHex(iOrd, 6)

.....

************************************************************
* pa...@ix.netcom.com   software eng/dev/test/hack         *
* NetCrusher...Someone had to make the  AOL'rs look good!  *
************************************************************

Re:How to convert from ascii to Hex (and vice versa)


Quote
pa...@ix.netcom.com (Dalan) wrote:
>var
>  sOrigVal  :  String[1];
>begin
>   sOrigVal := edit1.text;
>   iOrd     := Ord(sOrigVal); {This fail because Ord
>                           won't take a string var}
>   Memo1.Text := 'The Hex Value is ' + IntToHex(iOrd, 6)

Did you tried this ?

Memo1.Text:='TheHex Value is' + IntToHex(Ord(edit1.text[1]),2);

It should work and eliminate all unecessary intermediate variables.

Regards,

____________________

Yves MATHIEU
Keerbergen - Belgium
Yves.Math...@ping.be

Re:How to convert from ascii to Hex (and vice versa)


In article <42c704$...@ping1.ping.be>,
   Yves.Math...@ping.be (Yves Mathieu) wrote:
Quote
>pa...@ix.netcom.com (Dalan) wrote:

>>var
>>  sOrigVal  :  String[1];
>>begin
>>   sOrigVal := edit1.text;
>>   iOrd     := Ord(sOrigVal); {This fail because Ord
>>                               won't take a string var}
>>   Memo1.Text := 'The Hex Value is ' + IntToHex(iOrd, 6)

>Did you tried this ?

>Memo1.Text:='TheHex Value is' + IntToHex(Ord(edit1.text[1]),2);

>It should work and eliminate all unecessary intermediate variables.

>Regards,

It works.  I feel stupid.  Thanks.

************************************************************
* pa...@ix.netcom.com   software eng/dev/test/hack         *
* NetCrusher...Someone had to make the  AOL'rs look good!  *
************************************************************

Re:How to convert from ascii to Hex (and vice versa)


Quote
pa...@ix.netcom.com (Dalan) wrote:
>>>I'm converting some of my VB tools to Delphi.  I have a testing tool
>>>written in VB that converts text to its Hex value, and/or Hex values
>to
>>>ascii.  This is so easy to do in VB (and even easier in C), that I
>must
>>>be missing something obvious.  

>>Try  Ord() which is the logical equivalent of ASC() or ASCII().

>>  if ( Ord('A') = 65) then IFoundIt := TRUE;

>>Hope this helps
>>_______________________________________________________________________

>I'm still stuck.  I had tried Ord, but I assume that I've missed a step.
>For now, imagine a simplified version of this app that contains an edit
>box, a memo field, and a command button; and that I want to enter 1 char
>into the edit box, then display its hex value in the memo field.  

>var
>  sOrigVal  :  String[1];
>begin
>   sOrigVal := edit1.text;
>   iOrd     := Ord(sOrigVal); {This fail because Ord
>                           won't take a string var}
>   Memo1.Text := 'The Hex Value is ' + IntToHex(iOrd, 6)

>.....

Try something like this (I'm writing off the top of my head):

var
  sOrigVal, hexVal: string[10];  {room for a value like '17423', etc.}
  code, iVal:integer;

sOrigVal := edit1.text;
Val(sOrigVal, iVal, code);
if code = 0 then  {successful conversion to integer}
  hexVal := IntToHex(iVal)
else
  hexVal := 'Error';

and define the routine IntToHex like this:

function IntToHex(w: word):string;
const
  hexdigits:array[0..15] of char = '0123456789ABCDEF';
var
  hibyte, lobyte: byte;
begin
  hibyte:= w div 256;
  lobyte:= w mod 256;
  IntToHex:=   hexdigits[hibyte div 16]
             + hexdigits[hibyte mod 16]
             + hexdigits[lobyte div 16]
             + hexdigits[lobyte mod 16];
end;

Hope this helps.

Dave Shortt
wy...@wyatt.com

Re:How to convert from ascii to Hex (and vice versa)


Well, INTEGER to HEX (string) Conversion is simple:

Function HEX(a:byte):String;
CONST c:string[20]='0123456789ABCDEF';
Begin
 hex:=c[1+(a and $F0)shr 4]+c[1+(a and $0f)]
end;

Function Wordhex(a:word):String;
begin
  Wordhex:=hex(hi(a))+hex(lo(a));
end;

Function Longhex(a:Longint):String;
var s:string;
begin
  s:=wordhex (  (a and $ffff0000)shr 16)+
           wordhex(a and $FFFF);
  longhex:=s;
end;

...And HEX to integer is even simpler:

Function hextoInt(a:string):Longint;
var e:integer;L:longint;
begin
 VAL ('$'+a,L,e);
 Hextoint:=L;
end;

Hope this helps,

 Arthur Hoornweg.

Other Threads