Integer to code and back

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom

    #1

    Integer to code and back

    I need to convert an integer to a unique code string consisting only of
    capital letters and numbers.
    I also need to be able to convert it back to an integer again.

    Example:

    10 =DFG56JKG4 =10

    How do I accomplish that?
    Googling has not helped.

    Regards



  • Dan M

    #2
    Re: Integer to code and back

    On Wed, 03 Jan 2007 19:05:14 +0100, Tom wrote:
    I need to convert an integer to a unique code string consisting only of
    capital letters and numbers.
    I also need to be able to convert it back to an integer again.
    >
    Example:
    >
    10 =DFG56JKG4 =10
    >
    How do I accomplish that?
    Googling has not helped.
    >
    Regards
    What is the logic used to map from integer to string?

    Comment

    • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

      #3
      Re: Integer to code and back

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1

      Dan M wrote:
      On Wed, 03 Jan 2007 19:05:14 +0100, Tom wrote:
      >
      >10 =DFG56JKG4 =10
      >>
      >How do I accomplish that?
      >
      What is the logic used to map from integer to string?
      I guess that Tom has absolutely no idea on how to do that.

      Viable possibilities:
      - - A hash table.
      - - A simetric encryption function.

      - --
      - ----------------------------------
      Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

      http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
      MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
      Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net
      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.6 (GNU/Linux)

      iD8DBQFFnAFRR56 dWuhgxGgRAtXIAK CC1sid+P5QVExaU xpJF0pGqpyhoACf YoA7
      UhwYA0Cl03R6ZUw G+8C1jMw=
      =K7s3
      -----END PGP SIGNATURE-----

      Comment

      • Tom

        #4
        Re: Integer to code and back

        >>
        >What is the logic used to map from integer to string?
        >
        I guess that Tom has absolutely no idea on how to do that.
        Exactly!

        I have triede base64_decode / encode but it fails as it uses non
        alphanumerics.
        So does base32

        Viable possibilities:
        - - A hash table.
        Hmm doesn't ring a bell, and google gives me oodles of stuff that is not
        directly related.
        Could you please provide me with an example in pseudo code or something?
        - - A simetric encryption function.
        Well, same result when googling. The concept exists but apparently everybody
        knows it so well they don't explain it but just refer to it :-D
        An example please?

        Thanks for answering!

        Tom


        Comment

        • Michael Fesser

          #5
          Re: Integer to code and back

          ..oO(Tom)
          >I need to convert an integer to a unique code string consisting only of
          >capital letters and numbers.
          >I also need to be able to convert it back to an integer again.
          >
          >Example:
          >
          >10 =DFG56JKG4 =10
          Just curious: Why do you need that? The integer itself is unique and
          consists only of numbers ... so what's the purpose of the "encoding"?

          Micha

          Comment

          • =?UTF-8?B?SXbDoW4gU8OhbmNoZXogT3J0ZWdh?=

            #6
            Re: Integer to code and back

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            Tom wrote:
            >- - A hash table.
            >
            Hmm doesn't ring a bell, and google gives me oodles of stuff that is not
            directly related.
            Could you please provide me with an example in pseudo code or something?
            Basically, you generate a table that you store somewhere, and relates every
            integer you can think of with its hash.

            Got a new integer? Calculate the hash and store it in the table. Got a hash?
            Check if it exists in the table.

            How you exactly hash the integers is your problem, tough.
            >- - A simetric encryption function.
            >
            Well, same result when googling. The concept exists but apparently
            everybody knows it so well they don't explain it but just refer to it :-D
            An example please?
            RTFM. php.net/mcrypt

            - --
            - ----------------------------------
            Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

            Un ordenador no es un televisor ni un microondas, es una herramienta
            compleja.
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.6 (GNU/Linux)

            iD8DBQFFnB3ER56 dWuhgxGgRArsGAJ 0WTtAWZfRU+TfGW f8ZtNWC1Y+KMACf cXBu
            QH8OlXlAYsPnd1l NVrkgHlQ=
            =8jDe
            -----END PGP SIGNATURE-----

            Comment

            • Michael Fesser

              #7
              Re: Integer to code and back

              ..oO(Iván Sánchez Ortega)
              >Basically, you generate a table that you store somewhere, and relates every
              >integer you can think of with its hash.
              A hash is not unique.

              Micha

              Comment

              • Tom

                #8
                Re: Integer to code and back

                Got a new integer? Calculate the hash and store it in the table. Got a
                hash?
                Check if it exists in the table.
                Ah, good idea. Thanks.
                >>- - A simetric encryption function.
                >>
                >Well, same result when googling. The concept exists but apparently
                >everybody knows it so well they don't explain it but just refer to it :-D
                >An example please?
                >
                RTFM. php.net/mcrypt

                1) How was I supposed to link "simetric encryption" to "mcrypt" ??? :-D :-D
                2) Are you sure you it shouldn't be spelled "symmetric" ?

                Tom


                Comment

                • Tom

                  #9
                  Re: Integer to code and back


                  "Michael Fesser" <netizen@gmx.de wrote in message
                  news:rn6op2tglc g7uda14oblqpm7c k8bkefthj@4ax.c om...
                  .oO(Tom)
                  >
                  >>I need to convert an integer to a unique code string consisting only of
                  >>capital letters and numbers.
                  >>I also need to be able to convert it back to an integer again.
                  >>
                  >>Example:
                  >>
                  >>10 =DFG56JKG4 =10
                  >
                  Just curious: Why do you need that? The integer itself is unique and
                  consists only of numbers ... so what's the purpose of the "encoding"?
                  The purpose is to use it in links.
                  Each code will link to a given row in a database and then show an image.
                  But I don't want script kiddies to fetch all images iteratively :-)

                  Tom


                  Comment

                  • Moot

                    #10
                    Re: Integer to code and back

                    Michael Fesser wrote:
                    .oO(Tom)
                    >
                    I need to convert an integer to a unique code string consisting only of
                    capital letters and numbers.
                    I also need to be able to convert it back to an integer again.

                    Example:

                    10 =DFG56JKG4 =10
                    >
                    Just curious: Why do you need that? The integer itself is unique and
                    consists only of numbers ... so what's the purpose of the "encoding"?
                    >
                    Micha
                    I'm curious as well...a number is already unique, what benefit are you
                    looking to get from the encoding?

                    Heck, if all you want is a way to convert a number to an alphanumeric
                    string and back reliably without any kind of obfuscation, then you
                    could just convert the int to hex and voila, you've got an alphanumeric
                    string that contains 0-9 and A-F which can easily be converted back to
                    decimal notation.

                    - Moot

                    Comment

                    • Kimmo Laine

                      #11
                      Re: Integer to code and back

                      "Tom" <no@spam.please wrote in message
                      news:459c2564$0 $49209$14726298 @news.sunsite.d k...
                      2) Are you sure you it shouldn't be spelled "symmetric" ?

                      I'm pretty sure it's "symmetric" , but let's find out, shall we...
                      Type 2 keywords and click on the 'Fight !' button. The winner is the one which gets best visibility on Google.


                      "symmetric" 28,000,000 results vs. "simetric" 104,000 results...

                      --
                      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


                      Comment

                      • Toby Inkster

                        #12
                        Re: Integer to code and back

                        Michael Fesser wrote:
                        A hash is not unique.
                        For small[1] integers $i, md5($i) should be unique.

                        ____
                        1. Say, lower than 1,000,000,000

                        --
                        Toby A Inkster BSc (Hons) ARCS
                        Contact Me ~ http://tobyinkster.co.uk/contact

                        Comment

                        • Toby Inkster

                          #13
                          Re: Integer to code and back

                          Tom wrote:
                          10 =DFG56JKG4 =10
                          >
                          How do I accomplish that?
                          How about this...

                          define('MYSECRE T','Some unguessable string');

                          function int2code ($i)
                          {
                          $md5 = strtoupper(md5( MYSECRET.$i));
                          $mdStart = substr($md5, 0, 4);
                          $mdEnd = substr($md5, 28, 4);
                          return $mdStart.$i.$md End;
                          }

                          function code2int ($code)
                          {
                          preg_match('/^([A-F0-9]{4})([0-9]+)([A-F0-9]{4})$/', $code, $matches);
                          $i = $matches[2];

                          $md5 = strtoupper(md5( MYSECRET.$i));
                          $mdStart = substr($md5, 0, 4);
                          $mdEnd = substr($md5, 28, 4);
                          if ($mdStart.$i.$m dEnd==$code)
                          return $i;

                          return FALSE; // Invalid code!
                          }

                          --
                          Toby A Inkster BSc (Hons) ARCS
                          Contact Me ~ http://tobyinkster.co.uk/contact

                          Comment

                          • Toby Inkster

                            #14
                            Re: Integer to code and back

                            Iván Sánchez Ortega wrote:
                            Viable possibilities:
                            - - A hash table.
                            - - A simetric encryption function.
                            Possibility three:
                            - including the unencrypted integer in the query string, but signing it.

                            I've posted example code for this in my reply to Tom's post.

                            --
                            Toby A Inkster BSc (Hons) ARCS
                            Contact Me ~ http://tobyinkster.co.uk/contact

                            Comment

                            • Kimmo Laine

                              #15
                              Re: Integer to code and back

                              "Toby Inkster" <usenet200701@t obyinkster.co.u kwrote in message
                              news:j20v64-v48.ln1@ophelia .g5n.co.uk...
                              Michael Fesser wrote:
                              >
                              >A hash is not unique.
                              >
                              For small[1] integers $i, md5($i) should be unique.

                              Unique yes, but md5 is one-way, and the op wanted a way to convert integers
                              to strings and back to integers. How do you convert an md5 digest to
                              integer?

                              --
                              "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                              http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                              spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


                              Comment

                              Working...