Board index » delphi » Hex to ASCII ??

Hex to ASCII ??

Looking for a routine to convet hex values like %20 into their ascii
equivalents.  I realize this should be simple, but nothing I've tried
works.  Any help would be greatly appreciated.

Darcy

 

Re:Hex to ASCII ??


Quote
"Darcy" <m...@mypc.com> wrote in message

news:3ac91cfc.1895526693@west.usenetserver.com...

Quote
> Looking for a routine to convet hex values like %20 into their ascii
> equivalents.  I realize this should be simple, but nothing I've tried
> works.  Any help would be greatly appreciated.

I presume you're trying to convert a hex string into an integer (word,
whatever) type?

Here's a routine that should work for converting a hex string of up
to four digits into a word:

function Hex4ToWord(s: string): word;
const
  values: array[1..4] of word = (4096, 256, 16, 1);
  digits = '0123456789ABCDEF';
var
  i: integer;
  t: string[4];
  htw: word;
begin
  Hex4ToWord := 0;
  if length(s) > 4 then
  begin
    writeln('Error in Hex4ToWord - string too long!');
    Exit
  end;
  t := s;
  while length(t) < 4 do
    t := '0' + t;
  htw := 0;
  for i := 1 to 4 do
    if Pos(t[i], digits) = 0 then
      begin
        writeln('Error in Hex4ToWord - not a hex string!');
        Exit
      end
    else
      htw := htw + values[i] * (Pos(t[i], digits) - 1);
  Hex4ToWord := htw
end;

It shouldn't be hard to generalise this for larger types and signed
hex strings.

--
The Scarlet Manuka

Re:Hex to ASCII ??


The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote in message
news:9abohq$5re$1@fang.dsto.defence.gov.au...

Quote

> Here's a routine that should work for converting a hex string of up
> to four digits into a word:

snipped...

Perhaps for instructional purposes that will be good but try this
for ease of implementation (converts up to 8 digits to a longint):

  function HexToInt(const S: string): longint;
  var
    N  : longint;
    Err: integer;
  begin
    val('$'+S, N, Err);
    if Err = 0 then  HexToInt := N
               else  HexToInt := 0;  { illegal hex value }
  end; {HexToInt}

Re:Hex to ASCII ??


Quote
"Lester Hanger" <lest...@icon.co.za> wrote in message

news:3ac98030$0$226@hades.is.co.za...

Quote

> Perhaps for instructional purposes that will be good but try this
> for ease of implementation (converts up to 8 digits to a longint):

[snip wrapper around Val()]

Instructional purposes are good; that way the OP will be able to
solve similar problems on his own. If he needs to convert strings
from a base not supported by Val(), your method will not be of
much help.

--
The Scarlet Manuka

Re:Hex to ASCII ??


JRS:  In article <9abohq$5r...@fang.dsto.defence.gov.au> of Tue, 3 Apr
2001 13:58:02 in comp.lang.pascal.borland, The Scarlet Manuka

Quote
<sa...@maths.uwa.edu.au> wrote:
>  for i := 1 to 4 do
>    if Pos(t[i], digits) = 0 then
>      begin
>        writeln('Error in Hex4ToWord - not a hex string!');
>        Exit
>      end
>    else
>      htw := htw + values[i] * (Pos(t[i], digits) - 1);

ISTM better to start with the most significant digit, add that to HTW,
multiply HTW by base, and repeat for further digits.  There is then no
need for a table of powers of 16, no need to pad with zeroes, and all
lengths in range are automatically handled.

If the parameter s were a "const", a string copy would be saved.

Unless the input is machine-produced, it would probably be a good idea
to UpCase the characters in it; it is convenient to be able to type in
lower case.

When writing a table of powers of 16, ISTM safer to use $1, $10, $100,
$1000, etc.

However, all that is the answer to a different question; the originator
wanted to be able to convert Hex such as %20 to ASCII - presumably from
some form of Web scripting or equivalent.  By ASCII he(?) presumably
means Pascal type char.  In WWW programs/demime.pas, I use

function DeHex(C : char) : byte ;
var B : byte ;
begin
  B := Pos(C+'', '0123456789ABCDEF') ;              <-- "+''" not needed
  if B=0 then FAIL ;
  DeHex := Pred(B) end {DeHex} ;

  Value := char( ( DeHex(S[By+1]) shl 4 ) + DeHex(S[By+2]) ) ;
  Inc(By, 2) ;

where the '%' is at S[By].  For just two characters a loop seemed not
worthwhile.  The "char" is an essential part of the answer, though.  In
demime.pas, it is known that C will be upper case, from MIME.

Another way would be to replace the '%' by '$', and to use Val.

        Val('$21', byte(Ch), J) ;

does what is needed, on limited test, with Ch being of type char.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
  PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:Hex to ASCII ??


In article <3ac98030$0$...@hades.is.co.za>,

Quote
Lester Hanger <lest...@icon.co.za> wrote:
>The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote in message
>news:9abohq$5re$1@fang.dsto.defence.gov.au...

>> Here's a routine that should work for converting a hex string of up
>> to four digits into a word:

>snipped...

>Perhaps for instructional purposes that will be good but try this
>for ease of implementation (converts up to 8 digits to a longint):

>  function HexToInt(const S: string): longint;
>  var
>    N  : longint;
>    Err: integer;
>  begin
>    val('$'+S, N, Err);
>    if Err = 0 then  HexToInt := N
>               else  HexToInt := 0;  { illegal hex value }

Zero is a very bad marker for illegal value.

Osmo

Other Threads