Recent

Author Topic: Converting Hex string to binary string  (Read 26000 times)

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Converting Hex string to binary string
« Reply #30 on: December 19, 2021, 02:23:43 am »
I would take the opportunity and make three functions, which should limit moving around strings. Plus you get two extra conversion routines for free. I know the system lib provides it all, but its fun and educational. If I find time I will write the functions tomorrow.
It's only logical.

munair

  • Hero Member
  • *****
  • Posts: 884
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Converting Hex string to binary string
« Reply #31 on: December 19, 2021, 09:47:47 am »
As promised.
Code: Pascal  [Select][+][-]
  1. {$mode objFPC}
  2.  
  3. program hex2bin;
  4.  
  5. function dec2bin(d: longword): string;
  6.   var
  7.     n: byte = 65;
  8. begin
  9.   setlength(result, 64);
  10.   if d = 0 then
  11.     begin
  12.       dec(n);
  13.       result[n] := '0';
  14.     end;
  15.   while d > 0 do
  16.     begin
  17.       dec(n);
  18.       result[n] := chr(d mod 2 + 48);
  19.       d := d div 2;
  20.     end;
  21.   result := copy(result, n, 65 - n);
  22. end;
  23.  
  24. function hex2dec(const h: string): longword;
  25. var
  26.   b: byte;
  27.   i: word;
  28. begin
  29.   result := 0;
  30.   for i := 1 to length(h) do
  31.     begin
  32.       b := ord(h[i]);
  33.       case b of
  34.         97..102:          { a..f = 10..15}
  35.           b := b - 87;
  36.         65..70:            { A..F = 10..15 }
  37.           b := b - 55;
  38.         48..57:
  39.           b := b - 48;
  40.       end;
  41.       result := result * 16 + b;
  42.     end;
  43. end;
  44.  
  45. function hex2bin(h: string): string;
  46. begin
  47.   result := dec2bin(hex2dec(h));
  48. end;
  49.  
  50.  
  51. begin
  52.   //writeln(hex2dec('3b7'));
  53.   //writeln(dec2bin(951));
  54.   writeln(hex2bin('3b7'));
  55. end.
  56.  
« Last Edit: December 20, 2021, 01:19:16 pm by munair »
It's only logical.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Converting Hex string to binary string
« Reply #32 on: December 19, 2021, 01:13:41 pm »
Needed a break from what I was working on. Here is the result:
Code: Pascal  [Select][+][-]
  1. function H2B(const h:string):string;
  2. const
  3.   bins:array[$0..$F] of string =('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1010','1011','1100','1101','1110','1111');
  4.  
  5. type
  6.   TNibbles=bitpacked record
  7.     case boolean of
  8.       False:(c:char);
  9.       True: (L:$0..$F; H:$0..$F)
  10.   end;
  11.  
  12. var
  13.   b:PDWord;
  14.   n:TNibbles;
  15.   i:Integer;
  16. begin
  17.   SetLength(Result,Length(h) shl 2);
  18.   b:=@Result[1];
  19.   for i:=1 to Length(h) do
  20.   begin
  21.     n.c:=h[i];
  22.     case n.H of
  23.       3:  begin if n.L < 10      then Move(bins[0+n.L][1],b^,4) else exit('Error') end;
  24.       4,6:begin if n.L in [1..6] then Move(bins[9+n.L][1],b^,4) else exit('Error') end;
  25.       else;
  26.         Exit('Error');
  27.     end;
  28.     inc(b);
  29.   end;
  30. end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Converting Hex string to binary string
« Reply #33 on: December 19, 2021, 07:40:26 pm »
@jamie, looks nice, but it doesn't seem to produce binary string.

Kays

  • Hero Member
  • *****
  • Posts: 618
  • Whasup!?
    • KaiBurghardt.de
Re: Converting Hex string to binary string
« Reply #34 on: December 19, 2021, 08:56:32 pm »
[…] One item I can't remember is the clause used to inform the compiler registers […]
This
Code: Pascal  [Select][+][-]
  1. end ['rax', 'rcx'];
is only necessary for certain registers in pure assembly routines like yours.

And if you’re already writing assembly, you definitely want to process (at least) eight Bytes, eight chars at once, because the compiler generated code using Pascal source code definitely beats your implementation.
Yours Sincerely
Kai Burghardt

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Converting Hex string to binary string
« Reply #35 on: December 19, 2021, 09:17:46 pm »
Jamie, your code does not give "binary string".

It does not, for instance, produce '1110' when given 'E'

Your code is Hex String to Decimal, or did I miss something?

440bx

  • Hero Member
  • *****
  • Posts: 5576
Re: Converting Hex string to binary string
« Reply #36 on: December 19, 2021, 09:29:34 pm »
I don't understand here.

The idea was to take a HEX string of chars and generate a binary of it.
a binary string, i.e, a string of '0's and '1's - not the binary number represented by a hex string.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Seenkao

  • Hero Member
  • *****
  • Posts: 711
    • New ZenGL.
Re: Converting Hex string to binary string
« Reply #37 on: December 20, 2021, 03:44:52 am »
Needed a break from what I was working on. Here is the result:
Code: Pascal  [Select][+][-]
  1. function H2B(const h:string):string;
  2. const
  3.   bins:array[$0..$F] of string =('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001','1010','1011','1100','1101','1110','1111');
  4.  
  5. type
  6.   TNibbles=bitpacked record
  7.     case boolean of
  8.       False:(c:char);
  9.       True: (L:$0..$F; H:$0..$F)
  10.   end;
  11.  
  12. var
  13.   b:PDWord;
  14.   n:TNibbles;
  15.   i:Integer;
  16. begin
  17.   SetLength(Result,Length(h) shl 2);
  18.   b:=@Result[1];
  19.   for i:=1 to Length(h) do
  20.   begin
  21.     n.c:=h[i];
  22.     case n.H of
  23.       3:  begin if n.L < 10      then Move(bins[0+n.L][1],b^,4) else exit('Error') end;
  24.       4,6:begin if n.L in [1..6] then Move(bins[9+n.L][1],b^,4) else exit('Error') end;
  25.       else;
  26.         Exit('Error');
  27.     end;
  28.     inc(b);
  29.   end;
  30. end;
Можно немного проще
Eng: Can be a little simpler  ::)
Code: Pascal  [Select][+][-]
  1. var
  2.   bb: integer;
  3.   ...
  4. for i := 1 to Length(h) do
  5.   begin
  6.     bb := Byte(h[i]);
  7.     case bb of
  8.         48..57: Result := Result + bins[bb - 48];
  9.         65..70: Result := Result + bins[bb - 55];
  10.         97..102: Result := Result + bins[bb - 87];
  11.       else begin
  12.         Result := 'Error';
  13.         exit;
  14.       end;
  15.     end;
  16.   end;      

Иногда простые вещи могут быть более эффективными и понятными. Но ваше решение мне понравилось!
Eng: Sometimes simple things can be more effective and understandable. But I liked your decision!  ;)
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Converting Hex string to binary string
« Reply #38 on: December 20, 2021, 04:53:22 am »
Yes, definitely.

 

TinyPortal © 2005-2018