Web Analytics Made Easy -
StatCounter parseInt - CodingForum

Announcement

Collapse
No announcement yet.

parseInt

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • parseInt

    parsInt() converts a string to a hexadecimal number

    is there a function to convert a string to a binary number?
    http://www.bluephoenix.uni.cc/

  • #2
    There is now, it's called parseBin. Just keep this bit of script in your pages and you'll be OK.

    I named the function parseBin because it's easy to remember and is like the built in parseInt.
    It even returns the value that you pass it so you can do things like:

    var number=parseBin(1024);

    I've tried to demonstrate that in the links on the page.

    Edit: You should know that the binary number that is returned is just a base-10 number that only happens to contain 1's and 0's. If you want to do any sums with binary numbers in then you would have to do the sum first and then convert the answer to binary.
    If you intend to convert large numbers to binary then it would be best if the binary number was returned as a string because otherwise the binary number would be huge and would look something like this:

    1.1001011011101010e+26
    Attached Files
    Last edited by lavalamp; Feb 14, 2004, 08:42 PM.
    AMD Athlon "Barton" XP 3200+ (11*200.5MHz = 2.2GHz)
    BFG GeForce 6800 Ultra OC 256Mb
    3 * Kingston 256Mb PC3200 DDR400
    Seagate Barracuda 120Gb Ultra ATA-100 and Seagate Barracuda 160Gb, SATA-150

    Comment


    • #3
      id doesnt work:

      give me a wrong number but thanks anyways

      click on 10 to see what i mean
      Attached Files
      http://www.bluephoenix.uni.cc/

      Comment


      • #4
        1024 .toString(2)

        or:

        var someNumber; // set to whatever
        someNumber.toString(2)
        jasonkarldavis.com

        Comment


        • #5
          thanks
          http://www.bluephoenix.uni.cc/

          Comment


          • #6
            I didn't know you could do that with toString but in any case I guess it's sorted now, in defence of my script though...

            Originally posted by lavalamp
            If you intend to convert large numbers to binary then it would be best if the binary number was returned as a string because otherwise the binary number would be huge
            In this case, you entered an absolutely huge number to start with.
            AMD Athlon "Barton" XP 3200+ (11*200.5MHz = 2.2GHz)
            BFG GeForce 6800 Ultra OC 256Mb
            3 * Kingston 256Mb PC3200 DDR400
            Seagate Barracuda 120Gb Ultra ATA-100 and Seagate Barracuda 160Gb, SATA-150

            Comment


            • #7
              but how can i conver absolutly huge numbers?
              http://www.bluephoenix.uni.cc/

              Comment


              • #8
                Well since most CPU's are 32bit you're limited to a maximum number of:

                plus or minus 2 * 10^32 - 1

                I think the .toString(2) would convert that number to binary, but if you're looking to convert numbers bigger than that you're a bit stuffed.
                AMD Athlon "Barton" XP 3200+ (11*200.5MHz = 2.2GHz)
                BFG GeForce 6800 Ultra OC 256Mb
                3 * Kingston 256Mb PC3200 DDR400
                Seagate Barracuda 120Gb Ultra ATA-100 and Seagate Barracuda 160Gb, SATA-150

                Comment

                Working...
                X