# Board index » delphi » Convert a String to HEX

## Convert a String to HEX

Hi.
I am searching for some code to convert a string to hex.
This is what I want:

string:= 'Geir';
I want the output on the screen to be:
47 65 69 72

Can anyone help me with some code to do this??

Best regards
Geir O. Andersen

## Re:Convert a String to HEX

Hi!

##### Quote
Geir Andersen wrote:

> Hi.
> I am searching for some code to convert a string to hex.
> This is what I want:

> string:= 'Geir';
> I want the output on the screen to be:
> 47 65 69 72

> Can anyone help me with some code to do this??

This is a three-step operation:
1) Split the string into characters.
2) Convert each individual character into an integer
(using the "ord()" function).
3) Write each integer in hexadecimal coding.

If your compiler does not offer a notation for step 3,
you have to code it yourself, using "div 16" and "mod 16"
to get (the integer value of) the hexadecimal digits
and then comparing them to (decimal) 9 or 10 for the
decision to write a digit '0'..'9' or a char 'a'..'f'.

HTH,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
(speaking only for himself)
mailto: jo...@sql.de

## Re:Convert a String to HEX

I came up with the following:

Function Byte2Hex(numb : Byte): String;

Const
HexChars : Array[0..15] of Char = '0123456789ABCDEF';

begin
Byte2Hex[0] := #2;
Byte2Hex[1] := HexChars[numb shr 4];
Byte2Hex[2] := HexChars[numb and 15];
end;

procedure conv2val(s : string);
var
i: integer;
begin
i:= 1;
for i:= 1 to length(s) do
begin
int:= ord(s[i]);
write(byte2hex(int),' ');
end;
end;

procedure hex;
begin
while not eof(infile) do
begin
conv2val(fileline);
end;
end;

The Byte2Hex function I found on the Internet.

This works like a charm.
I want the output on the screen to be limited to 15 characters pr. line.
How can I do this?
Is there an easy way, or is it a lot of programming.

Geir

"Joerg Bruehe" <jo...@sql.de> skrev i melding
news:3DBE594C.4F941066@sql.de...

##### Quote
> Hi!

> Geir Andersen wrote:

> > Hi.
> > I am searching for some code to convert a string to hex.
> > This is what I want:

> > string:= 'Geir';
> > I want the output on the screen to be:
> > 47 65 69 72

> > Can anyone help me with some code to do this??

> This is a three-step operation:
> 1) Split the string into characters.
> 2) Convert each individual character into an integer
>    (using the "ord()" function).
> 3) Write each integer in hexadecimal coding.

> If your compiler does not offer a notation for step 3,
> you have to code it yourself, using "div 16" and "mod 16"
> to get (the integer value of) the hexadecimal digits
> and then comparing them to (decimal) 9 or 10 for the
> decision to write a digit '0'..'9' or a char 'a'..'f'.

> HTH,
> Joerg Bruehe

> --
> Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
>      (speaking only for himself)
> mailto: jo...@sql.de

## Re:Convert a String to HEX

##### Quote
Geir Andersen <disk...@nospam.online.no> wrote:
> I am searching for some code to convert a string to hex.

Amply covered in

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland
Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html

## Re:Convert a String to HEX

##### Quote
Timo Salmi wrote:
> Geir Andersen <disk...@nospam.online.no> wrote:

> > I am searching for some code to convert a string to hex.

> Amply covered in

>  165966 Jan 8 2000 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip

You should point out that that is ONLY for the non-standard Turbo
dialect of Pascal, and thus not portable to standards conforming
systems.

An (untested) conforming version could be:

PROCEDURE wrt2hex(VAR f : text; c : char)

TYPE
hexindex = 1..16;

VAR
hi, lo   : hexindex;
hexchars : PACKED ARRAY[hexindex] OF char;

BEGIN
hexchars = '0123456789abcdef';
hi = (ord(c) DIV 16) MOD 16 + 1;
lo = ord(c) MOD 16 + 1;
write(f, hexchars[hi], hexchars[lo]);
END; (* wrt2hex *)

and you can move the hexindex type and hexchars array outside the
procedure (for efficiency) provided you initialize hexchars
somewhere.

--
Chuck F (cbfalco...@yahoo.com) (cbfalco...@worldnet.att.net)
Available for consulting/temporary embedded and systems.

## Re:Convert a String to HEX

##### Quote
CBFalconer  <cbfalco...@worldnet.att.net> wrote:
> Timo Salmi wrote:
> >  165966 Jan 8 2000 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
> You should point out that that is ONLY for the non-standard Turbo
> dialect of Pascal, and thus not portable to standards conforming
> systems.

The algorithms quite often are portable. That's crucial. One then
just has to formulate for the syntax discrepancies (just as you
kindly did).

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland
Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html

## Re:Convert a String to HEX

Hi Geir!

##### Quote
Geir Andersen wrote:

> Thanks for the quick answer.

You are welcome - but please stop top-post, full-quote ("TOFU").

##### Quote
> [...]
> I have an additional question..
> I want the output on the screen to be limited to 15 characters pr. line.
> How can I do this?
> Is there an easy way, or is it a lot of programming.

Introduce a global variable, count the characters written (IMO:
within "conv2val"), and issue a 'writeln'.

HTH,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
(speaking only for himself)
mailto: jo...@sql.de

## Re:Convert a String to HEX

"Joerg Bruehe" <jo...@sql.de> schreef in bericht
news:3DBE9A99.B5CB588A@sql.de...

##### Quote
> Hi Geir!

> Geir Andersen wrote:

> > Thanks for the quick answer.

> You are welcome - but please stop top-post, full-quote ("TOFU").

> > [...]
> > I have an additional question..
> > I want the output on the screen to be limited to 15 characters pr.
line.
> > How can I do this?
> > Is there an easy way, or is it a lot of programming.

> Introduce a global variable, count the characters written (IMO:
> within "conv2val"), and issue a 'writeln'.

The simplest way is probably

Uses crt;
begin
window(1,1,15,25);
writeln ('Hello this is a line that is longer than fif{*word*249}
characters');
end.

## Re:Convert a String to HEX

##### Quote
Geir Andersen wrote:

... snip ...

> I want the output on the screen to be limited to 15 characters pr.
> line. How can I do this?
> Is there an easy way, or is it a lot of programming.

If your system complies with the standards (Turbo doesn't) it is
very simple:

write(f, stringvar : 15);

will write the first 15 chars of the string variable only.  It
will pad a string shorter than 15 chars with blanks.  f is a
textfile.

DO NOT TOPPOST.

--
Chuck F (cbfalco...@yahoo.com) (cbfalco...@worldnet.att.net)
Available for consulting/temporary embedded and systems.