Solved

STRTOHEX

Posted on 2006-06-22
3
4,344 Views
Last Modified: 2008-01-09
Yo guys im stuck here

I need to convert a text to hex - not a big deal if you use inttohex(strtoint(text),2);

Now the problem:

how do I convert a text like :

my_text := 1234567890123;
showmessage(inttohex(strtoint(my_text),2)); // this will raise an exception in strtoint(my_text) since the number is higher then 2147483647

Question: Is there a way to convert a string with up to 10 digits into hexadecimal ? how to do it ?



thx in adv
0
Comment
Question by:furmiga
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 500 total points
ID: 16965106
I think I've got this right :-)
try it out

procedure TForm1.Button1Click(Sender: TObject);
    var
        my_text: string;
    begin
        my_text := '1234567890123';
        showmessage(Int64ToHex(StrToInt64(my_text)));
    end;

procedure Int64ToHex(bigi: int64): string;
    var
        lo1: integer;
    begin
        result := '';
        while bigi > 0 do
        begin
            lo1 := lo(bigi);
            result := IntToHex(lo1, 2) + result;
            bigi := bigi shr 8;
        end;
        result := '$' + result;
    end;
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16965124
what i'm doing is using an int64 (because it can store larger numbers) and then working out the hex for 1 byte at a time
"lo( )" gets the bottom byte, and "shr 8" moves my "big number" to the right 1 byte (8 bits), effectively dropping off the last byte I just grabbed
I append the hex values, and stick a $ at the start
0
 
LVL 28

Expert Comment

by:2266180
ID: 16965295
here is an alternative I've just spent some time on implementing that takes an integer string as uinput no matter how many digits:

procedure bigdiv16(int:string; var d,m:string);
var n,i,k:integer;
begin
  i:=1;
  repeat
    n:=strtoint(copy(int,1,i));
    inc(i);
  until (n div 16>0) or (i>length(int));
  if i>length(int) then
  begin
    d:='0';
    m:=int;//=inttostr(n);
    exit;
  end;
  d:='';
  k:=1;
  repeat
    n:=strtoint(copy(int,1,i-1+k-1));
    if n div 16 > 9 then
    begin
      dec(i);
      continue;
    end;
    d:=d+chr((n div 16)+48);
    if (n<16) and (length(int)>i-1+k-1) then
    begin
      inc(i);
      continue;
    end;
    delete(int,1,i-1+k-1);
    int:=inttostr(n mod 16)+int;
    k:=length(inttostr(n mod 16));
  until length(int)=k;
  m:=int;
end;

function strtohex(int:string):string;
var d,m:string;
begin
  d:=int;
  result:='';
  repeat
    bigdiv16(d,d,m);
    result:=inttohex(strtoint(m),1)+result;
  until d='0';
end;


my initial algorithm was much simpler, but there were some bugs. and that is why I put in thos nasty inc/decs. it's 4:30 am here. I should go to sleep cause I make ugly code :D

peace
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Internet Explorer View Settings Question 15 111
Mobile Keyboard covers the display of the TMemo 3 88
select query - oracle 16 100
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question