Help with Hex Calculator

ftawrld

New member
Joined
Dec 28, 2010
Messages
1
Programming Experience
Beginner
Hi Guys,

I'm making a hex calculator and I need to be able to do few things

1.) hex * 2
2.) Hex / 2
3.) Hex xor Hex

Can any1 tell me how to do this.

Also I use function Hex(textbox1.text) to convert to hex, but that actually takes away the front zero how can i make sure the ones that are missing the zero to be there.

Thank you
FTA
 
First up, I'd suggest not using Hex. You should first validate the data to make sure that it is valid. If you are already doing that elsewhere then I'd suggest converting your String to an Integer like so:
VB.NET:
Dim number As Integer = Convert.ToInt32(text, 16)
The converse operation, i.e. converting a an Integer to a hexadecimal String, looks like this:
VB.NET:
Dim text As String = Convert.ToString(number, 16)
If you need to validate an convert then you should do something like this:
VB.NET:
Dim number As Integer

If Integer.TryParse(text, Globalization.NumberStyles.AllowHexSpecifier, Nothing, number) Then
    MessageBox.Show(number.ToString(), "Decimal")
Else
    MessageBox.Show("Invalid Input")
End If
As for leading zeroes, numbers don't have leading zeroes. Numbers are just numbers. An Integer is a 32-bit binary value. Leading zeroes are only an issue when displaying the value of a number, which is done using Strings, so it's is only an issue when displaying data to the user. Once you have used Convert.ToString to convert your number to a String, you can use String.PadLeft to add leading zeroes if you desire.
 
Hex, Decimal, Octal, Binary, is just how the data is shown to the user... inside the processor is just an integer (maybe long or small integer).

So as long as you proper validate the input before using it and proper convert the output back to the on-screen representation your application requires (see jmcilhinney codes on those conversions), the rest math is just math.
+ - * / And Or Xor will work anyway!
 
Back
Top