convert between bases?

Baz047

New member
Joined
Dec 22, 2008
Messages
4
Programming Experience
Beginner
Hey everyone, I just started learning Visual Basic so please don't over-confuse me lol

I just need a little help, I have been searching google for hours trying to find out how to convert between bases. I have coded a calculator, where you type in numbers (in the format of decimal, hex or binary, choses by radio buttons). You then click convert and it converts what you typed in into the format you desire (specified by more radio button).

My question is, how would I go about doing this, I have found very lengthy code, that is highly confusing, has arrays, about 10 variables and stuff...but it is just too mind boggling for me.

Is there an easy way of doing this, like specifying the textbox to get the input from, and tell it that it's in base 10 say, then have it convert to either binary or hex (user specified)?

I also want to convert any of those 3 in ASCII format and output it to the user...how would that be done?

Thanks for any help given, and please, try an explain any code or suggestions as nooby as possible :-D
 
The bases are just string representations of the same numeric value. Convert class has an overload for ToString method where you can specify to which base the number string should display. Similar it has ToInt32 and such where you can specify which base the input string is. Examples:
VB.NET:
Dim i As Integer = 10
Dim binary As String = Convert.ToString(i, 2)
Dim hex As String = Convert.ToString(i, 16)
i = Convert.ToInt32(binary, 2)
i = Convert.ToInt32(hex, 16)
To go from for example base 2 to 16, you would convert the string to numeric value first, then convert that to the other base string.
 
Sorry to be a bother but could you give me a code example of how to go from 2 to 16? I'm not entirely sure what you mean.

Also, do you know how to convert to ASCII?
 
Following exactly what JohnH showed you earlier.

VB.NET:
Dim base2 As String = "1111"
Dim i As Integer = Convert.ToInt32(base2, 2)
Dim base16 As String = Convert.ToString(i, 16)
MessageBox.Show("Base2: " & base2 & Environment.NewLine & "Base10: " & i & Environment.NewLine & "Base16: " & base16)
 
Sorry to be a bother but could you give me a code example of how to go from 2 to 16? I'm not entirely sure what you mean.

Also, do you know how to convert to ASCII?

Encoding.ASCII.GetString(byte array)


You can also take your number and call Convert.ToChar() on it, though it's not "ASCII" per se because of the way code pages work
 
Back
Top