I dont mind bug: BinToHex implementation/description do not match !

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 11/25/04


Date: Thu, 25 Nov 2004 09:09:14 +0100

Hello,

The BinToHex procedure occurding to Delphi's help would return a 'null'
terminated text string.

This is not the case.

The implementation simply converts all binary values to hex values but
forgets to add a null terminated character.

Actually I don't really mind this bug.

Since it's a quite stupid prototype design anyway... why would anyway want
to terminate it with a #0 in the first place.

That would limit it's use somewhat to pchars only.

A better prototype would simply be this:

procedure BinToHex( const BinaryBuffer; var HexBuffer; BufSize: Integer);

Also it says "assembler" at the end... but the whole procedure is not in
assembler it's just in pascal hehe.

procedure BinToHex(Buffer, Text: PChar; BufSize: Integer); assembler;
const
  Convert: array[0..15] of Char = '0123456789ABCDEF';
var
  I: Integer;
begin
  for I := 0 to BufSize - 1 do
  begin
    Text[0] := Convert[Byte(Buffer[I]) shr 4];
    Text[1] := Convert[Byte(Buffer[I]) and $F];
    Inc(Text, 2);
  end;
end;

I guess in the past there was also an ASM version:

It's commented so maybe it mal functioned :D I dont know =D

{asm
        PUSH ESI
        PUSH EDI
        MOV ESI,EAX
        MOV EDI,EDX
        MOV EDX,0
        JMP @@1
@@0: DB '0123456789ABCDEF'
@@1: LODSB
        MOV DL,AL
        AND DL,0FH
        MOV AH,@@0.Byte[EDX]
        MOV DL,AL
        SHR DL,4
        MOV AL,@@0.Byte[EDX]
        STOSW
        DEC ECX
        JNE @@1
        POP EDI
        POP ESI
end;}

"
Call BinToHex to convert the binary value in a buffer into a string that is
its hexadecimal representation.

Buffer is a buffer of bytes that contains the binary value.

Text returns a null-terminated string that represents the value of Buffer as
a hexadecimal number.

BufSize is the size of Buffer. Text needs to point to a sequence of
characters that has at least 2*BufSize bytes because each hexadecimal
character represents two bytes.
"

P.S.:

!!!!! Fortunately I have the Delphi VCL source code !!!!! Otherwise I would
be pretty screwed by now !!!!! =D

Bye,
  Skybuck.



Relevant Pages

  • Re: I dont mind bug: BinToHex implementation/description do not match !
    ... assigning it to another string you may see this problem. ... > forgets to add a null terminated character. ... > to terminate it with a #0 in the first place. ... > Buffer is a buffer of bytes that contains the binary value. ...
    (alt.comp.lang.borland-delphi)
  • Re: Linux Security
    ... >> did you terminate the string you stacked with a newline character? ... >> address of the stack, which is what I presume you passed it. ... > work in a buffer overflow, and what I need to do in order to be able ...
    (comp.os.linux.security)
  • Re: proper way to determine string length
    ... If you call reador freadthe buffer is not guaranteed to be ... string, it probably won't be null-terminated and will probably ... forgetting to null terminate a string" unless you're going to try ... strings into a null-termination problem. ...
    (comp.unix.programmer)
  • RE: Help converting Buffer of Bytes to string
    ... You can Null terminate a byte array by ... > What is the best way of adding a null to a Buffer of Bytes. ... >> I note that you can null teminate a string by adding controlchar.null. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)