Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Best way to represent somewhat large values in a module

by sgifford (Prior)
on Jun 19, 2004 at 06:07 UTC ( [id://368123]=perlquestion: print w/replies, xml ) Need Help??

sgifford has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on some Perl modules to handle RFID tags. The tag IDs can be up to 96 bits. I'm not sure what the best way to accept these IDs as parameters, and return them, and wanted to ask for some ideas.

Right now, I'm just accepting and returning hex strings, and treating the tag IDs as somewhat opaque strings. But it's possible somebody will want to do bitwise operations on the tags (e.g. to & it with some mask), in which case they have to convert them from hex into something they can compute with. And my module is spending a lot of time converting to and from hex.

Math::BigInt seems like overkill for this, since for the most part representing the tags as strings will work fine. And using pack and unpack makes them unprintable, which is annoying.

So my question is, if you were using a module that dealt with a 96-bit ID number in various ways, what would you find the most convenient representation to work with? Or, if you were designing a module that had to work with 96-bit ID numbes, what factors would you take into consideration when thinking about the representation?

Any thoughts are appreciated!

Replies are listed 'Best First'.
Re: Best way to represent somewhat large values in a module
by tachyon (Chancellor) on Jun 19, 2004 at 06:57 UTC

    You say you are converting to hex but then go on to say And using pack and unpack makes them unprintable, which is annoying.

    Why not just treat them as strings (these will typically be unprintable) and upack them into hex as requrired for printable output. You can use bitmap operators on strings (with care)

    my $num = "\000\001\002\003\004\005\006\007\010\011\012\013"; my ($bin) = unpack "B*", $num; print length $bin, ": ", $bin, "\n"; print printable($num), "\n"; $num |= "\000\001\002\003\004\005\006\007\010\377\012\013"; print printable($num), "\n"; sub printable { unpack "H*", $_[0] }

    cheers

    tachyon

      Thanks for your thoughts, tachyon.
      You say you are converting to hex but then go on to say And using pack and unpack makes them unprintable, which is annoying.
      Right, I meant that's why I'm converting to hex instead of using pack and unpack. Those whole-string bit operations look pretty tempting, though...Perhaps the right thing to do is only convert to and from hex when I get data from the user, and store the data as bitstrings, using code like the above. Hmmm...

        There are some 'gotchas' when you do $str BIT_OP $str but for things like masks you should be OK. Sometimes it works like you expect, other times not. It is fairly magical that it works at all!

        cheers

        tachyon

Re: Best way to represent somewhat large values in a module
by Zaxo (Archbishop) on Jun 19, 2004 at 06:15 UTC

    I think you have the right idea treating them as strings, For manipulating bits, try vec.

    Update: For clarification, treating them as 12 byte strings, no character conversion. unpack will help isolate that.

    After Compline,
    Zaxo

Re: Best way to represent somewhat large values in a module
by wufnik (Friar) on Jun 19, 2004 at 17:40 UTC
    tachyon's code above is esp. neat, tho i hope that he sets certain provisos against it's use in a RFID context ie: please please please don't start flashing dimensional warp generator adverts at me when i enter barnes & noble wearing the latest RFID enabled clothes...
    ...wufnik

    -- in the world of the mules there are no rules --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-28 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found