hex to another base

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

    hex to another base

    I am trying to find a php solution to encoding an md5 hex string into
    another base.

    Using ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz12345678 90 as a
    characters for a base 62
    i.e.
    A = 1
    a = 27
    0 = 62
    00 = 3844
    000 = 238328

    This should make the hex string much shorter.

    1. Does anyone understand what I'm trying to do here?
    2. Has this wheel already been invented?
    3. Any ideas about the most efficient way to encode and decode this kind of
    thing?

    Thanks,

    Nel


  • Justin Koivisto

    #2
    Re: hex to another base

    Nel wrote:[color=blue]
    > I am trying to find a php solution to encoding an md5 hex string into
    > another base.
    >
    > Using ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz12345678 90 as a
    > characters for a base 62
    > i.e.
    > A = 1
    > a = 27
    > 0 = 62
    > 00 = 3844
    > 000 = 238328
    >
    > This should make the hex string much shorter.
    >
    > 1. Does anyone understand what I'm trying to do here?
    > 2. Has this wheel already been invented?
    > 3. Any ideas about the most efficient way to encode and decode this kind of
    > thing?[/color]



    However, the order that is used on that for the characters are:

    0123456789ABCDE FGHIJKLMNOPQRST UVWXYZabcdefghi jklmnopqrstuvwx yz

    (which seems more logical than having the digits at the end)

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • Francois Bonzon

      #3
      Re: hex to another base

      On 2006-02-20 17:44:26 +0100, Justin Koivisto <justin@koivi.c om> said:
      [color=blue]
      > Nel wrote:[color=green]
      >> I am trying to find a php solution to encoding an md5 hex string into
      >> another base.
      >>
      >> Using ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz12345678 90 as
      >> a characters for a base 62
      >> i.e.
      >> A = 1
      >> a = 27
      >> 0 = 62
      >> 00 = 3844
      >> 000 = 238328
      >>
      >> This should make the hex string much shorter.
      >>
      >> 1. Does anyone understand what I'm trying to do here?
      >> 2. Has this wheel already been invented?
      >> 3. Any ideas about the most efficient way to encode and decode this
      >> kind of thing?[/color]
      >
      > http://www.pgregg.com/projects/php/b...conversion.php
      >
      > However, the order that is used on that for the characters are:
      >
      > 0123456789ABCDE FGHIJKLMNOPQRST UVWXYZabcdefghi jklmnopqrstuvwx yz
      >
      > (which seems more logical than having the digits at the end)[/color]

      If you're trying to save memory, IMHO the best way to represent your
      MD5 hex string is with binary data. Check out the functions pack() to
      encode to binary data, and bin2hex() to decode. Example:

      <?

      echo $hex_string = md5('abc123'), "\n";
      $binary_data = pack('H*', $hex_string);
      echo bin2hex($binary _data), "\n";

      // Outputs:
      // e99a18c428cb38d 5f260853678922e 03
      // e99a18c428cb38d 5f260853678922e 03

      ?>

      Comment

      • Nel

        #4
        Re: hex to another base

        "Francois Bonzon" wrote in message news:43f9f404@e pflnews.epfl.ch ...[color=blue]
        >
        > If you're trying to save memory, IMHO the best way to represent your MD5
        > hex string is with binary data. Check out the functions pack() to encode
        > to binary data, and bin2hex() to decode. Example:
        >
        > <?
        >
        > echo $hex_string = md5('abc123'), "\n";
        > $binary_data = pack('H*', $hex_string);
        > echo bin2hex($binary _data), "\n";
        >
        > // Outputs:
        > // e99a18c428cb38d 5f260853678922e 03
        > // e99a18c428cb38d 5f260853678922e 03
        >
        > ?>[/color]
        Hi Francois,

        In this cas I am trying to pass a shorter URL and moving from base 16 to 62
        knocks off about 10 chars from the URL. Not as many as I'd hoped for, but
        it's a definate improvement.

        Nel


        Comment

        • Nel

          #5
          Re: hex to another base

          "Justin Koivisto" <justin@koivi.c om> wrote in message
          news:U_2dnXUTjt pCbGTenZ2dnUVZ_ tmdnZ2d@onvoy.c om...[color=blue]
          > Nel wrote:[color=green]
          >> I am trying to find a php solution to encoding an md5 hex string into
          >> another base.
          >>
          >> Using ABCDEFGHIJKLMNO PQRSTUVWXYZabcd efghijklmnopqrs tuvwxyz12345678 90 as a
          >> characters for a base 62
          >> i.e.
          >> A = 1
          >> a = 27
          >> 0 = 62
          >> 00 = 3844
          >> 000 = 238328
          >>
          >> This should make the hex string much shorter.
          >>
          >> 1. Does anyone understand what I'm trying to do here?
          >> 2. Has this wheel already been invented?
          >> 3. Any ideas about the most efficient way to encode and decode this kind
          >> of
          >> thing?[/color]
          >
          > http://www.pgregg.com/projects/php/b...conversion.php
          >
          > However, the order that is used on that for the characters are:
          >
          > 0123456789ABCDE FGHIJKLMNOPQRST UVWXYZabcdefghi jklmnopqrstuvwx yz
          >
          > (which seems more logical than having the digits at the end)
          >
          > --
          > Justin Koivisto, ZCE - justin@koivi.co m
          > http://koivi.com[/color]
          Perfect Justin ;)
          Thanks

          Nel


          Comment

          • Tim Roberts

            #6
            Re: hex to another base

            "Nel" <nelly@ne14.co. NOSPAMuk> wrote:[color=blue]
            >
            >In this cas I am trying to pass a shorter URL and moving from base 16 to 62
            >knocks off about 10 chars from the URL. Not as many as I'd hoped for, but
            >it's a definate improvement.[/color]

            You don't have to "hope", you can compute this. The number of bits per
            digit is the logarithm in base 2 of the number base. Base 16 packs 4 bits
            per digit. Base 62 packs 5.9 bits per digit. The string will be about 1/3
            shorter.
            --
            - Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            Working...