As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Very large numbers in PHP

AgentBryantAgentBryant CTRegistered User regular
edited December 2010 in Help / Advice Forum
The situation is that I would essentially like to pass around 70 boolean variables via GET in php to a webpage. Now, rather than actually using 70 different variables, which would make a ludicrously long url, I'd like to use bit flags to pass them all as one integer. Of course, 2^70 seems too large to be in the scope of what I'm used to handling. So, what I want to ask is if there's a way to (easily enough) convert a 70-digit binary number to an integer, then converted back to binary to extract each value.

Note that I do not want to use php sessions or forms to pass these values, because I want to be able to do this strictly by url.

AgentBryant on

Posts

  • Options
    evilmrhenryevilmrhenry Registered User regular
    edited December 2010
    Too long for an integer. There are add-ons for "big math", but personally, I'd just stick it in a string.

    evilmrhenry on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited December 2010
    why not just do three 20 digit masks

    Jasconius on
  • Options
    InfidelInfidel Heretic Registered User regular
    edited December 2010
    Yeah, divy it up imo.

    Infidel on
    OrokosPA.png
  • Options
    BlazeFireBlazeFire Registered User regular
    edited December 2010
    So I have zero PHP experience so I probably should not be replying, but is there any way you can cast the bits to a character? You would end up with 9 characters (2 extra bits that would never be assigned based on your boolean values).

    BlazeFire on
  • Options
    SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2010
    You could always convert it to another base, that's possibly more compact (hex, oct?)

    Seguer on
  • Options
    virgilsammsvirgilsamms Registered User regular
    edited December 2010
    base 64 encoding is pretty much designed for this - encoding binary data in ascii format that wont be messed around during transport.

    http://php.net/manual/en/function.base64-encode.php

    virgilsamms on
  • Options
    SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2010
    Generally going from numeric to ascii results in more character usage, and if he is trying to send 2^70 numbers, that's a lot of characters.

    URLs can only contain so many characters in them, which is why he really needs a compact conversion.

    Some sort of codec would be best here (one that outputs a URL friendly string)

    Seguer on
  • Options
    evilmrhenryevilmrhenry Registered User regular
    edited December 2010
    Seguer wrote: »
    Generally going from numeric to ascii results in more character usage, and if he is trying to send 2^70 numbers, that's a lot of characters.

    URLs can only contain so many characters in them, which is why he really needs a compact conversion.

    Some sort of codec would be best here (one that outputs a URL friendly string)

    70 numbers, not 2^70.

    evilmrhenry on
  • Options
    SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2010
    Oh, whoops.
    Of course, 2^70 seems too large to be in the scope of what I'm used to handling

    This tripped me up.

    It's not 70 numbers, it is a
    70-digit binary number

    Which can either mean 70 0s and 1s, or 70x however many bits he is using.

    If it is 8bit, 70x8 = 560 0s and 1s.. which is a huge decimal number (1.1793632577567317e+167)

    Now that I think about it, the 70th variable IS going to 2^70 (or can be)


    AgentBryant: could you provide more context? Maybe there is a better way to do what you are trying to do.

    Seguer on
  • Options
    virgilsammsvirgilsamms Registered User regular
    edited December 2010
    It's a 70 bit bitmask, so can be represented as a long byte stream, 9 bytes in this case.

    Here's how it would work with 24 bits, but can be any amount

    Bitmask: 010000010100000101000001
    Base64 encode that sucker: QUFB
    Make URL: www.AgentBryantsFancySite.com?data=QUFB
    Base64 decode QUFB: 010000010100000101000001

    Voila! Large bitstream passed via url.

    That said, like Seguer mentioned it feels like there might be a better way.

    virgilsamms on
  • Options
    AgentBryantAgentBryant CTRegistered User regular
    edited December 2010
    Essentially, the user will choose which of ~70 objects will appear on a particular page, preferably through URL so it can be linked. So for each object, its either there, or it isn't. 2^70 combinations. Ideally, I wanted to convert the string of 1's and 0's to an integer, to be converted back to binary in order to read off the 1's and 0's again, but of course these are very large numbers to work with for integers. What I was wondering is if there was an easy enough way to do this, say, by variable type. It seems as though there's not, however I think some of the ideas here will help be accomplish what I want to.

    I highly doubt this is the best way to accomplish what I want, but it seems to work.

    Thanks for the replies.

    AgentBryant on
  • Options
    SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited December 2010
    Alright I'd say a bitmask is not what you want here.

    Unless you're on a 64 bit system, the Integer will be too small I believe, to handle a 2^70 binary number being converted (hell, can 64 bit handle it?)

    Don't worry about all the ones that are off, or all the ones that are on.

    Choose one (or even have it be dynamic!) and do something like this:

    item : id
    item1 : 1
    item2 : 2
    ...
    item69 : 69
    item70: 70

    blah.php?data=1,2,6,12,45,78,34,12&state=on

    That way you can use state = off, if there are more "on" than "off" to have a shorter URL (potentially)

    You may want to be using arrays here too at some point, depending on how you've setup these objects and your data.

    If you really wanted to you could still base64 encode that data parameter :)

    Seguer on
Sign In or Register to comment.