Solved

convert from base 10 to base 32

Posted on 2003-11-03
7
2,925 Views
Last Modified: 2012-06-22
I was curious if anyone knew how I could take a base 32 number convert it to base 10 and then back to base 32..

going from base 16 to 10 back to 16 would also be nice
0
Comment
Question by:jayrod
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:wellilein
ID: 9671037
   Private Function base10to16(ByVal number As Integer) As String
        Dim hexnumbers As String = "0123456789ABCDEF"
        Dim hex As String = ""
        Dim remainder As Integer
        Do
            remainder = number Mod 16
            number = number \ 16
            hex = hexnumbers.Chars(remainder) & hex
        Loop While number > 0
        base10to16 = hex
    End Function

    Private Function base10to32(ByVal number As Integer) As String
        Dim hexnumbers As String = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
        Dim hex As String
        Dim remainder As Integer
        Do
            remainder = number Mod 32
            number = number \ 32
            hex = hexnumbers.Chars(remainder) + hex
        Loop While number > 0
        base10to32 = hex
    End Function

    Private Function base32to10(ByVal thirtytwo As String) As Integer
        Dim hexnumbers As String = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
        Dim i As Integer
        Dim number As Integer = 0
        For i = 0 To Len(thirtytwo) - 1
            number = number * 32
            number = number + hexnumbers.IndexOf(thirtytwo.Chars(i))
        Next
        base32to10 = number
    End Function

I think you'll can make base16to10 by your own, having a look at base32to10
0
 
LVL 2

Accepted Solution

by:
wellilein earned 500 total points
ID: 9671130
Sorry, it was VB. C# follows:

public static string base10to16(int number )
{
      string hexnumbers = "0123456789ABCDEF";
      string hex = "";
      int remainder;
      do
      {
            remainder = number % 16;
            number = number / 16;
            hex = hexnumbers[remainder] + hex;
      }
      while (number>0);
      return hex;
}
      public static string base10to32(int number )
{
      string hexnumbers = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
      string hex = "";
      int remainder;
      do
      {
            remainder = number % 32;
            number = number / 32;
            hex = hexnumbers[remainder] + hex;
      }
      while (number>0);
      return hex;
}

public static integer base32to10(string thirtytwo)
{
      string hexnumbers = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
      int number = 0;
      for (int i= 0;i<thirtytwo.Length;i++)
      {
            number *= 32;
            number += hexnumbers[i];
      }
      return number;
}

base16to10 left open in case it is homework...

It's also possible to have only two functions:
public static string base10toX(int number, int base)
public static int baseXto10(string number, int base)
and then convert to even silly bases like 15 or 23.
0
 
LVL 3

Author Comment

by:jayrod
ID: 9671818
bah it's not homework :P Thank you for the info. I'm using this to make primary keys in my database let me get this to work and I'll hand out the points.

Thank you very much
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Author Comment

by:jayrod
ID: 9672490
I am using the base 16 methods. When I convert 23 to base 16 I get 2A
When trying to get 2A back into base 10 I get 817.

any ideas why?

here are my methods

public static string base10to16(int number)
{
      string hexnumbers = "0123456789ABCDEF";
      string hex = "";
      int remainder;
      do
      {
            remainder = number % 16;
            number = number / 16;
            hex = hexnumbers[remainder] + hex;
      }
      while (number>0);
      return hex;

}


public static int base16to10(string input)
{
      string hexnumbers = "0123456789ABCDEF";
      int number = 0;
      for (int i= 0;i<input.Length;i++)
      {
            number *= 16;
            number += hexnumbers[i];
      }
      return number;
}
0
 
LVL 2

Expert Comment

by:wellilein
ID: 9676978
I should test my postings more often.
base10to16 works for me, it returns 23dec = 17hex and 65535dec=FFFFhex
In base16to10 I have successfully ignored the input (string input is never used for calculation)
Here's a corrected version:

public static int base16to10(string input)
{
      string hexnumbers = "0123456789ABCDEF";
      int number = 0;
      for (int i= 0;i<input.Length;i++)
      {
            number *= 16;
            number += hexnumbers.IndexOf(input[i]);
      }
      return number;
}

I'm really sorry.

Also, this method won't work for negative numbers.
0
 
LVL 3

Author Comment

by:jayrod
ID: 9787607
I'm still confused on how the base16to10 method is supposed to work. I tested this with the numbers 2A and 42 and I'm not getting what I put in.

It's been a while.. sorry for that.. so I'll increase the points for ya
0
 
LVL 2

Assisted Solution

by:wellilein
wellilein earned 500 total points
ID: 9830961
If you want to convert "2A" to decimal, this is, how it works:

number = 0;
// for loop iteration with i = 0
number = number * 16; // number is still 0
number = number + 2; // because string "2" is found at index 2 in the string "0123456789ABCDEF"
// for loop with iteration i = 1
number = number *16; // number is now 32
number = number + 10; // because string "A" is found at index 10 in the string "0123456789ABCDEF"
// for loop is exiting, because i is no longer smaller than the length of "2A"
return number; // number is 42

The other way round:

hex = "";
// for loop iteration no. 1
remainder = number % 16; // remainder is 10 because 42 divided by 16 is 2 remainding 10
number = number / 16; // number is 2 because it is integer
hex = "A" + hex; // because "A" is the string at position 10 in "0123456789ABCDEF"
// hex == "A" at this step
// for loop iteration no. 2
remainder = number % 16; // remainder is 2 because 2 divided by 16 is 0 remainding 2
number = number / 16; // number is 0 because it is integer
hex = "2" + hex; // because "2" is the string at position 2 in "0123456789ABCDEF"
// hex is "2" + "A" == "2A"
// for loop is exiting because number is 0
return hex; // hex is "2A"

So this will do fine, doesn't it?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now