Board index » delphi » Help with ASCII to HEX conversion please

Help with ASCII to HEX conversion please

Hello again!

I have a program which lifts portions of one file and deposits them into
another.  Most of this is working fine.  Unfortunately I have to both read
and write to the files in HEX and the values I have assigned appear to be
ASCII.  I either need to discover a different way of reading from and
writing to the files (ie. in HEX) or I will need a procedure to convert the
variables from ASCII to HEX.

Any help with this would (again) be greatly appreciated.

TC.

 

Re:Help with ASCII to HEX conversion please


Quote
Tim Carey wrote:

> Hello again!

> I have a program which lifts portions of one file and deposits them into
> another.  Most of this is working fine.  Unfortunately I have to both read
> and write to the files in HEX and the values I have assigned appear to be
> ASCII.  I either need to discover a different way of reading from and
> writing to the files (ie. in HEX) or I will need a procedure to convert the
> variables from ASCII to HEX.

> Any help with this would (again) be greatly appreciated.

> TC.

I am sure you dont mean hex but binary.

Try the
Val(AnyStr:String; Var I:Integer{or whatever},Var ErrLoc:Integer)
procedure.
http://www.geocities.com/SiliconValley/2926/tp.html

Regards,
Franz Glaser
http://members.eunet.at/meg-glaser

Re:Help with ASCII to HEX conversion please


Ing. Franz Glaser (meg-gla...@eunet.at) wrote:

Quote
: Tim Carey wrote:

: > I have a program which lifts portions of one file and deposits them into
: > another.  Most of this is working fine.  Unfortunately I have to both read
: > and write to the files in HEX and the values I have assigned appear to be
: > ASCII.  I either need to discover a different way of reading from and
: > writing to the files (ie. in HEX) or I will need a procedure to convert the
: > variables from ASCII to HEX.
:
: I am sure you dont mean hex but binary.

      Just in case he did mean Decimal to Hexidecimal,
here is a procedure I wrote.   I think I only needed
the range up to $FFF, but up to $FFFF should work by
inserting variable b1 into the output string.

Procedure Decimal_2_Hexidecimal;
var       sr,dr,msbr,lsbr:     real;
          s,d,msb,lsb,n1,n2,n3,n4,b1,b2,b3,b4:      integer;
          ask:                                      char;
begin         (* INput = wnum, OUTput = SST -  globals   *)
   If wnum < 0 then begin minN:= 1; wnum:= 0 - wnum; dr:= wnum; end
               else begin minN:= 0; wnum:= wnum; dr:= wnum; end;
   msbr:= int(dr/256); lsbr:= dr - (msbr*256);
   LSB:= TRUNC(LSBR); MSB:= TRUNC(MSBR);
   n1:= msb div 16; if n1 > 9 then b1:= n1 + 55 else b1:= n1 + 48;
   n2:= msb - (n1 * 16); if n2 > 9 then b2:= n2 + 55 else b2:= n2 + 48;
   n3:= lsb div 16; if n3 > 9 then b3:= n3 + 55 else b3:= n3 + 48;
   n4:= lsb - (n3 * 16); if n4 > 9 then b4:= n4 + 55 else b4:= n4 + 48;
   SST:= '$' + chr(b2) + chr(b3) + chr(b4);
   if minN = 1 then SST:= chr($2d) + SST;
end;

Ken Fischer

---

Other Threads