Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1781

# String to Hex

Hi all,

How can i convert a string (for example: 123abc) to hex??
i'm using delphi 4.

Thanks,
Vincent
0
Vincentg
• 6
• 5
• 2
• +1
1 Solution

Commented:
Hi,

There is no Hex type (as far as I know):

function HexToInt(value: string): integer;
begin
result := StrToInt('\$'+value);
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
i: integer;
begin
s := '123abc';
i := HexToInt(s);
ShowMessage(IntToStr(i));
end;

Regards, Geo
0

Author Commented:
sorry forgot something..

The output needs te be something like 95,bb,45,09,7e,15,a3,71

0

Commented:
maybe somthing like this

procedure TForm1.Button1Click(Sender: TObject);
var i : integer;
begin
edit2.text := '';
for i := 1 to length(edit1.Text) do
edit2.Text := edit2.Text + format('%x ',[ord(edit1.text[i])]);
end;

meikl ;-)
0

Commented:
I believe you want to convert from a string to the hex-equivalent, with comma-separators?

Remember that the resultant string will be nearly three times the length of the original!

function hexof(h : char) : string;
const
hexchars : string[16] = '0123456789ABCDEF';
begin
hexof := hexchars[succ(ord(h) div 16] +
hexchars[succ(ord(h) mod 16];
end;

function inhex(s : string) : string;
begin
if s = '' then
inhex := ''
else
if length(s) = 1 then
inhex := hexof(s[1])
else
inhex := hexof(s[1]) + ',' + inhex(copy(s,2,length(s)));
end;

sure, this could be cleaned up a bit - but it'll do what you appear to want!

...Bill
0

Author Commented:
sorry forgot something..

The output needs te be something like 95,bb,45,09,7e,15,a3,71

0

Commented:
see my sample above ;-)

it retruns for 123abc

31 32 33 61 62 63

if you want a comma,
just use instead of this line
edit2.Text := edit2.Text + format('%x ',[ord(edit1.text[i])]);

this line
edit2.Text := edit2.Text + format('%x,',[ord(edit1.text[i])]);

the result will then be

31,32,33,61,62,63,

meikl ;-)
0

Author Commented:
How do i "enter" the function from my button1click procedure??

What i really want is this:
I need to change an password in the registry on several NT machines. This password is hex. The program using this password is VNC. I want to make a program that converts text to hex and writes the output to a file (.reg)

{
Go into the registry and open up the
password key. You will see something like 275EA8D255B012F2 (hex for abc123).
Create a .reg  file with these lines

[HKEY_LOCAL_MACHINE\SOFTWARE\ORL\WinVNC3\Default]
}

This is what i have sofar:

procedure TForm1.Button1Click(Sender: TObject);
var cool : string;
arq : TFileStream;
begin
Cool := '[HKEY_LOCAL_MACHINE\SOFTWARE\ORL\WinVNC3\Default]' + #13#10 + '"Password"=hex:' + label3.caption;
Arq.Write(Pchar(Cool)^, Length(Cool));
Arq.Free;
end;

function hexof(h : char) : string;
const
hexchars : string[16] = '0123456789ABCDEF';
begin
hexof := hexchars[succ(ord(h) div 16)] +
hexchars[succ(ord(h) mod 16)];
end;

function inhex(s : string) : string;
begin
if s = '' then
inhex := ''
else
if length(s) = 1 then
inhex := hexof(s[1])
else
inhex := hexof(s[1]) + ',' + inhex(copy(s,2,length(s)));
end;

0

Commented:

becomes

...but I prefer kretzschmar's function - even if it generates a superfluous comma, and can produce a 1-character hex code!

{fixed & converted to function below}
function inttohex2(s : string) : string;
var
s2 : string;
for i := 1 to length(s) do
s2 := s2 + format('%2x,',[ord(s[i])]);
inttohex2 := copy(s2,1,pred(length(s2)));
end;

...Bill

0

Commented:
:-))
thanks for the fix, bill,
but in this case the questioner wasn't clear,
except the superflous comma

;-)
0

Author Commented:
No errors from delphi anymore but:

Why do i get 616263313233 when converting abc123

This should be:
275EA8D255B012F2 (hex for abc123)
0

Commented:
> ? 275EA8D255B012F2

why?
0

Author Commented:
I use winVNC for remote administration of my NT servers.
When i change the VNC password to abc123 it is changed in the registry of windows to "password:  Reg_Binair 27 5e a8 d2 55 b0 12 f2"

To make things easier i need a tool that generates a .reg (register) file with the converted code in it:

[HKEY_LOCAL_MACHINE\SOFTWARE\ORL\WinVNC3\Default]

According to a VNC forum the password should be in hex but when i use the code provided in this tread i get an other "hex" (616263313233) when converting abc123.

I hope you understand my "problem" better now.

Vincent
0

Commented:
? what will you archive ?

don't know what windows will additional do,
or how it writes binary, also maybe the vnc
does crypt the entry before storing into the registry

btw.
616263313233 is the correct hex-expression for abc123 uncrypted

btw. if i write abc123 into the registry
with writebinary,
i get

78 24 c2 00 78 24

meikl ;-)
0

Author Commented:
Thanks to all for helping

Vincent
0

## Featured Post

• 6
• 5
• 2
• +1
Tackle projects and never again get stuck behind a technical roadblock.