Hacks2Bin




  • This is a coworkers' implementation of Hex2Bin, in Perl.
    It's similar to sprintf("%02x", hex($hexStr));

    #####################################################################################
    #Hex2Bin - Convert HEX to Binary

    Input parameters: $HEX

    Returns: $Binary

    #####################################################################################
    sub Hex2Bin
    {
    my $hex = shift;
    my $binary;
    local $/ = '';
    my %h2b = (0 => "0000",
    1 => "0001", 2 => "0010", 3 => "0011",
    4 => "0100", 5 => "0101", 6 => "0110",
    7 => "0111", 8 => "1000", 9 => "1001",
    "a" => "1010", "b" => "1011", "c" => "1100",
    "d" => "1101", "e" => "1110", "f" => "1111",
    "A" => "1010", "B" => "1011", "C" => "1100",
    "D" => "1101", "E" => "1110", "F" => "1111",
    );
    $binary = $hex;
    $binary =~ s/(.)/$h2b{lc $1}/g;
    return $binary;
    }

    PS: First post, seems that editor needs explicit formatting to be added? Just used "pre" tag ... sorry about that.



  •  Hmf, he missed lowercase 0-9.



  • It has one more local variable than it needs, and the coercion to lowercase along with the doubled-up table entries for "a" through "f" is a little belt-and-braces, but it's scarcely a WTF. After all: it's correct, it's easy to see what it's doing, it will probably do it reasonably quickly, and it works for input hex strings of any length. What's not to like? The only thing I'd change is the (.) in the regexp, because boobies are sad without partners and using ([[:xdigit:]]) instead would allow input strings to include non-hex characters for formatting that carry over to the output.



  • @flabdablet said:

    but it's scarcely a WTF.
    The only WTF is that it is a case of unnecessarily rolling your own reimplementation of something built-in. It is exactly (I think) equivalent to sprintf('%0b', 4length($hexStr), hex($hexStr)); in the case of well-formed input. Both will fail, although in different ways, on input with non-hex-digit characters.



  • I think TRWTF is saying, "It's similar to sprintf("%02x", hex($hexStr));" because it's really like, sprintf("%b", hex($hexStr)).



  • @HardwareGeek said:

    @flabdablet said:
    but it's scarcely a WTF.
    The only WTF is that it is a case of unnecessarily rolling your own reimplementation of something built-in.
    Meh. It's Perl. TMTOWTDI.



  • @flabdablet said:

    It's Perl. TMTOWTDI.
    Of course, but why add yet another one?



  • @boomzilla said:

    I think TRWTF is saying, "It's similar to sprintf("%02x", hex($hexStr));" because it's really like, sprintf("%b", hex($hexStr)).

    Yes, my typo, not 02x but 08b. The input in our case is always 8 bits and the desired output is always 8 chars.



  • It must be just me, but my pet hate is overzealous quoting. There is no need for the quotes (especially double quotes) on the left of the "=>" fat comma. I even have a script in my editor to remove this type of quoting, along with changing $foo{'bar'} to just $foo{bar}. We use perl not php!


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.