Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8587919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:41:18+00:00 2026-06-11T22:41:18+00:00

How do I convert a string to base36 (0-9,a-z) in perl? I want to

  • 0

How do I convert a string to base36 (0-9,a-z) in perl?

I want to convert IP addresses to base36 with a one to one map, so I can use them in a URL.

127.0.0.1
=== string ==> 127000000001 === base36 ==>

72.98.234.11
=== string ==> 072098234011 === base36 ==>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:41:20+00:00Added an answer on June 11, 2026 at 10:41 pm

    Isn’t base 10 good enough? That gives a number in [0-4294967295] (1 to 10 digits). That’s only slightly longer than what you we were going to get from your way (1 to 8 digits).

    $ perl -E'say unpack "N", pack "C4", split /\./, $ARGV[0]' 127.0.0.1
    2130706433
    
    $ perl -E'say unpack "N", pack "C4", split /\./, $ARGV[0]' 72.98.234.11
    1214441995
    

    Completely reversible:

    $ perl -E'say join ".", unpack "C4", pack "N", $ARGV[0]' 2130706433
    127.0.0.1
    
    $ perl -E'say join ".", unpack "C4", pack "N", $ARGV[0]' 1214441995
    72.98.234.11
    

    You could convert that number to another base other than 10 using any number of modules on CPAN.

    base 10: 1-10 digits
    base 16: 1-8 digits    # Can be done very efficiently
    base 32: 1-7 digits    # Can be done very efficiently
    base 36: 1-7 digits
    base 62: 1-6 digits
    base 64: 1-6 digits    # Can be done very efficiently
    

    Base 16:

    sub pack_ip { sprintf "%X", unpack "N", pack "C4", split /\./, $_[0] }
    
    sub unpack_ip { join ".", unpack "C4", pack "N", hex $_[0] }
    

    Base 64: (This should be quite speedy. Faster even if ported to C.)

    my @syms = ('0'..'9', 'A'..'Z', 'a'..'z', '_', '-');
    my %syms = map { $sym[$_] => $_ } 0..$#syms;
    
    sub pack_ip {
       my $num = unpack "N", pack "C4", split /\./, $_[0];
       my $base64 = join '', @syms[
          ($num >> 30) & 0x3F,
          ($num >> 24) & 0x3F,
          ($num >> 18) & 0x3F,
          ($num >> 12) & 0x3F,
          ($num >>  6) & 0x3F,
          ($num >>  0) & 0x3F,
       ];
       $base64 =~ s/^A+(?!\z)//;
       return $base64;
    }
    
    sub unpack_ip {
       my $num;
       $num = ($num << 6) | $sym{$_}
          for split //, $_[0];
       return join ".", unpack "C4", pack "N", $num;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do i convert String www.mywebsite.com/firefox.txt to URL ? I wanted to use this
I want to convert an int to a string with base36 (0-9 and a-z).
Is there any free java library which I can use to convert string in
I want to convert String to Date in different formats. For example, I am
Possible Duplicate: How can I convert String to Int? public List<int> GetListIntKey(int keys) {
how convert string into Unicode string in Perl. I am looking some attribute in
I want to convert string Date into Timestamp in java. The following coding i
I want to convert string representation of real number to real type. I know
How to convert string from one charset to another in C++ in a portable
How can i convert String like 20100102 into datetime in a formate of dd/MM/yyyy?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.