Solved

# String to Hex

Posted on 2003-03-18
Medium Priority
1,770 Views
Hi all,

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

Thanks,
Vincent
0
Question by:Vincentg
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 6
• 5
• 2
• +1

LVL 17

Expert Comment

ID: 8159373
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 Comment

ID: 8159471
sorry forgot something..

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

Please a code sample.
0

LVL 27

Expert Comment

ID: 8159529
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

LVL 7

Accepted Solution

billious earned 150 total points
ID: 8159582
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 Comment

ID: 8159713
sorry forgot something..

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

Please a code sample.
0

LVL 27

Expert Comment

ID: 8159747
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 Comment

ID: 8160095
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 := TFileStream.Create('password.vnc', fmCreate);
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

LVL 7

Expert Comment

ID: 8160256

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

LVL 27

Expert Comment

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

;-)
0

Author Comment

ID: 8164918
No errors from delphi anymore but:

Why do i get 616263313233 when converting abc123

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

LVL 27

Expert Comment

ID: 8164944
> ? 275EA8D255B012F2

why?
0

Author Comment

ID: 8165049
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

LVL 27

Expert Comment

ID: 8165144
? 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 Comment

ID: 8172013
Thanks to all for helping

Vincent
0

## Featured Post

Question has a verified solution.

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

A lot of questions regard threads in Delphi. Â  One of the more specific questions is how to show progress of the thread. Â  Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to theâ€¦
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the inâ€¦
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
###### Suggested Courses
Course of the Month8 days, 20 hours left to enroll

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

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