Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Extracting Bit Fields (Again)

by pg (Canon)
on Oct 12, 2005 at 03:58 UTC ( [id://499361]=note: print w/replies, xml ) Need Help??


in reply to Extracting Bit Fields (Again)

Here is a simple base converter, with less than 10 lines of code, it gives you what you wanted and more. It is not difficult for you to continue from here and extract the specific digits, substring() if you wish.

use strict; use warnings; for (2 .. 9) { print "base $_: " . convert(162, $_), "\n"; } sub convert { my ($value, $base) = @_; my $converted = ""; while ($value) { $converted = ($value % $base) . $converted; $value = int($value / $base); } return $converted; }

This gives:

base 2: 10100010 base 3: 20000 base 4: 2202 base 5: 1122 base 6: 430 base 7: 321 base 8: 242 base 9: 200

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://499361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found