Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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

going from base 16 to 10 back to 16 would also be nice

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 = "0123456789ABCDEFGHIJKLMNO

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 = "0123456789ABCDEFGHIJKLMNO

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialThank you very much

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;

}

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.

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

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?

C#

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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

Loop While number > 0

base10to16 = hex

End Function

Private Function base10to32(ByVal number As Integer) As String

Dim hexnumbers As String = "0123456789ABCDEFGHIJKLMNO

Dim hex As String

Dim remainder As Integer

Do

remainder = number Mod 32

number = number \ 32

hex = hexnumbers.Chars(remainder

Loop While number > 0

base10to32 = hex

End Function

Private Function base32to10(ByVal thirtytwo As String) As Integer

Dim hexnumbers As String = "0123456789ABCDEFGHIJKLMNO

Dim i As Integer

Dim number As Integer = 0

For i = 0 To Len(thirtytwo) - 1

number = number * 32

number = number + hexnumbers.IndexOf(thirtyt

Next

base32to10 = number

End Function

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