in reply to Bit by a Hex String

I have a hex string that is 104 bits.

Huh? What exactly do you have? A bit string, or a string using hexadecimal numbers?

Oh, and for those wondering why I'm not using Math::BigInt, this script will be distributed, and it is likely that atleast one machine won't have that on it

Considering that Math::BigInt has been part of the standard distribution for a while, that seems unlikely. Furthermore, Math::BigInt lives on CPAN, so you should be able to distribute it with your program if you want to.

Abigail

Replies are listed 'Best First'.
Re^2: Bit by a Hex String
by abitkin (Monk) on Aug 29, 2003 at 14:28 UTC
    I have a hex string. Each charcter in a hex string represents 4 bits, thus my string length is 26 charcters. The number of bits was just to show that it could not be stored in a regular perl scalar.

    As for Math::BigInt being in the default install, honestly I didn't know that. I've never been able to find a list of what's included by default and from which version on.

    If I were to use Math::BigInt, how would I load the value in, as the documentation of the module does not describe loading hexidecimal values, just decimal values.

    Finally, as to the point of Math::BigInt living on Cpan, well that only works if you have access to the internet.

    This script is being put on number of machines that will be used in demo at a tradeshow, as a tool to use, in case of a problem with the application we are demoing. As such, I am not able to install it myself, but I'm sending the file to someone else, in marketing.

    For that reason I must make it as simple as possible, as people in marketing are not what I'd consider "power users." Since I will not be able to see the machines till they are at the tradeshow, and we will not have internet at the tradeshow, this makes cpan preaty much useless.
    ==
    Kwyjibo. A big, dumb, balding North American ape. With no chin.

      Here's a Math::BigInt solution.
      #!/usr/bin/perl use strict; use warnings; use Math::BigInt; $_ = "DEADBEEFBABEFACE1234567890"; my $num = Math::BigInt -> new (0); # Convert the number to a Math::Int decimal number. while (length) { my $chunk = substr $_, 0, 4, ""; $num *= 2 ** (4 * length $chunk); $num += hex $chunk; } # Multiply by 2, add 1. $num *= 2; $num += 1; # Convert the Math::BigInt number to hex. my @a; while ($num) { push @a => sprintf "%X" => $num % 16; $num /= 16; } my $new = join "" => @a; print $new, "\n";

      And here's an Inline::BC solution:

      #!/usr/bin/perl use strict; use warnings; use Inline 'BC'; print x("DEADBEEFBABEFACE1234567890"); __DATA__ __BC__ ibase = 16 obase = 10 define x (a) { return (a * 2 + 1) }

      Abigail

        Okay, first I need to say, I love your BC solution. It's simple, and elegant. That being said I did some playing around with BigInt, so here's the code I came up with:
        sub shift_bs{ $add = shift; $source = shift; $num1=Math::BigInt->new("0x".$source); $num1->blsft(1); $num1+=$add; $temp = $num1->as_hex(); $temp =~ /0x(.*)/; return $1; }
        Yeah, the $add is a legacy thing, but that's okay. I want to thank you especially, because you went to quite a bit of work to come up with that code.
        ==
        Kwyjibo. A big, dumb, balding North American ape. With no chin.