Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

convert/expand bytes to bits

RipperRipper Member Posts: 85
Hi there ...

I am trying to expand bytes (in a hex format) to bits (binary).

I used the hextobin command in delphi, but it doesn't seem to work ?

any suggestions ...

Regards

Comments

  • zibadianzibadian Member Posts: 6,349
    [b][red]This message was edited by zibadian at 2005-4-18 5:7:23[/red][/b][hr]
    : Hi there ...
    :
    : I am trying to expand bytes (in a hex format) to bits (binary).
    :
    : I used the hextobin command in delphi, but it doesn't seem to work ?
    :
    : any suggestions ...
    :
    : Regards
    :
    Here are several conversion functions, which I wrote some time ago:
    [code]
    function HexToInt(Hex: string): integer;
    var
    i: integer;
    begin
    Result := 0;
    case Length(Hex) of
    0: Result := 0;
    1..8: for i:=1 to Length(Hex) do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    else for i:=1 to 8 do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    end;
    end;

    function HexToInt64(Hex: string): int64;
    var
    i: integer;
    begin
    Result := 0;
    case Length(Hex) of
    0: Result := 0;
    1..16: for i:=1 to Length(Hex) do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    else for i:=1 to 16 do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    end;
    end;

    function Int64ToBin(Num: Int64; Length: integer): string;
    var
    i: Int64;
    begin
    i := 1;
    Result := '';
    while (i <= Num) or (system.Length(Result) < Length) do begin
    if Num and i = i then
    Result := '1' + Result
    else
    Result := '0' + Result;
    i := i * 2;
    end;
    end;

    function IntToBin(Num: cardinal; Length: integer): string;
    var
    i: cardinal;
    begin
    i := 1;
    Result := '';
    while (i <= Num) or (system.Length(Result) < Length) do begin
    if Num and i = i then
    Result := '1' + Result
    else
    Result := '0' + Result;
    i := i * 2;
    end;
    end;

    function BinToInt(Bin: string): integer;
    var
    i, j: Integer;
    begin
    Result := 0;
    j := 1;
    for i := Length( Bin ) downto 1 do
    begin
    Result := Result + StrToInt(Bin[i]) * j;
    j := j * 2;
    end;
    end;

    function BinToInt(Bin: array of boolean): cardinal; overload;
    var
    i: integer;
    BinStr: string;
    begin
    BinStr := '';
    for i := 0 to High(Bin) do
    if Bin[i] then
    BinStr := '1'+BinStr
    else
    BinStr := '0'+BinStr;
    Result := BinToInt(BinStr);
    end;

    function BinToInt64(Bin: array of boolean): Int64; overload;
    var
    i: integer;
    BinStr: string;
    begin
    BinStr := '';
    for i := 0 to High(Bin) do
    if Bin[i] then
    BinStr := '1'+BinStr
    else
    BinStr := '0'+BinStr;
    Result := BinToInt64(BinStr);
    end;

    function BinToInt64(Bin: string): Int64; overload;
    var
    i: integer;
    j: Int64;
    begin
    Result := 0;
    j := 1;
    for i := Length(Bin) downto 1 do
    begin
    if Bin[i] = '1' then
    Result := Result + j;
    j := j * 2;
    end;
    end;
    [/code]
    They are well tested and should work fine.
    They can also be found in the CodePedia.
  • KoppisKoppis Member Posts: 68
    [b][red]This message was edited by Koppis at 2005-4-18 12:36:31[/red][/b][hr]
    : [b][red]This message was edited by zibadian at 2005-4-18 5:7:23[/red][/b][hr]
    : : Hi there ...
    : :
    : : I am trying to expand bytes (in a hex format) to bits (binary).
    : :
    : : I used the hextobin command in delphi, but it doesn't seem to work ?
    : :
    : : any suggestions ...
    : :
    : : Regards
    : :
    : Here are several conversion functions, which I wrote some time ago:
    : [code]
    function HexToInt(Hex: string): integer;
    var
    i: integer;
    begin
    Result := 0;
    case Length(Hex) of
    0: Result := 0;
    1..8: for i:=1 to Length(Hex) do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    else for i:=1 to 8 do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    end;
    end;

    function HexToInt64(Hex: string): int64;
    var
    i: integer;
    begin
    Result := 0;
    case Length(Hex) of
    0: Result := 0;
    1..16: for i:=1 to Length(Hex) do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    else for i:=1 to 16 do
    Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1;
    end;
    end;

    function Int64ToBin(Num: Int64; Length: integer): string;
    var
    i: Int64;
    begin
    i := 1;
    Result := '';
    while (i <= Num) or (system.Length(Result) < Length) do begin
    if Num and i = i then
    Result := '1' + Result
    else
    Result := '0' + Result;
    [blue]i := i + i;[/blue] //i := i * 2;
    end;
    end;

    function IntToBin(Num: cardinal; Length: integer): string;
    var
    i: cardinal;
    begin
    i := 1;
    Result := '';
    while (i <= Num) or (system.Length(Result) < Length) do begin
    if Num and i = i then
    Result := '1' + Result
    else
    Result := '0' + Result;
    [red]Inc(i,i);[/red] //i := i * 2;
    end;
    end;

    function BinToInt(Bin: string): integer;
    var
    i, j: Integer;
    begin
    Result := 0;
    j := 1;
    for i := Length( Bin ) downto 1 do
    begin
    Result := Result + StrToInt(Bin[i]) * j;
    [red]Inc(j,j);[/red] //j := j * 2;
    end;
    end;

    function BinToInt(Bin: array of boolean): cardinal; overload;
    var
    i: integer;
    BinStr: string;
    begin
    BinStr := '';
    for i := 0 to High(Bin) do
    if Bin[i] then
    BinStr := '1'+BinStr
    else
    BinStr := '0'+BinStr;
    Result := BinToInt(BinStr);
    end;

    function BinToInt64(Bin: array of boolean): Int64; overload;
    var
    i: integer;
    BinStr: string;
    begin
    BinStr := '';
    for i := 0 to High(Bin) do
    if Bin[i] then
    BinStr := '1'+BinStr
    else
    BinStr := '0'+BinStr;
    Result := BinToInt64(BinStr);
    end;

    function BinToInt64(Bin: string): Int64; overload;
    var
    i: integer;
    j: Int64;
    begin
    Result := 0;
    j := 1;
    for i := Length(Bin) downto 1 do
    begin
    if Bin[i] = '1' then
    Result := Result + j;
    [blue]j := j + j;[/blue] //j := j * 2;
    end;
    end;[/code]

    I am harcore optimizer myself. I am not sure how to Inc with Int64 since I don't have D4 or newer. But with Integer, there are some possible things to do. I have marked my changes with red. They are not needed, but could boost speed while working on an ancient computer like mine :-).
    Here is something boring to read:
    "We can also consult Intel and AMD documents with latency and throughput tables. On P4 add & mov are 0.5 clock cycles latency and throughput, mul is 14-18 cycles latency and 5 cycles throughput. Shl is 4 clock cycles latency and 1 cycle throughput. The version chosen by Delphi is the most efficient on P4 and this will probably also be the case on Athlon and P3."

    Edit: Indeed, a + calculation may be faster than *. I marked those with blue. Crazy, isn't it? :-)
Sign In or Register to comment.