Question OKay, need help with hex

Tedious Grey

Member
Joined
Oct 13, 2008
Messages
7
Programming Experience
Beginner
Okay so, I'm making this big all-in-one utility, but there's three things I have not added to the project, and through all my googling, I have not seen 1 code example that will help yet.

The first thing is, I want to implement a hex calculator into my project.I have seen millions of regular calculators, but I have not found any script for a hex calc.

The second thing is, I need a function where a user inputs a text string into a box, and the action it takes it to take every 4 letters of that string, reverse it, then convert it to hex.
Basically what it would need to do is, if the user typed in:Visualbasic
It would need an ouput:
Set 1 = 0x75736956
Set 2 = 0x61626C61
Set 3 = 0x00636973


Lastly, I need a feature that when a user inputs a hex value, like say 120F, hitting a button will add 8800000 in HEX to that hex number.


If anybody here can help me out, or post some code examples that might work, I would be eternally grateful.
 
Hello.

You can pretty easy work with hex-numbers, since they are interpreted as normal numbers.

Writing a Hex-Calculator is the same thing as a regular calculator. All you have to do is treat the hex-numbers as normal numbers, f.e.:
VB.NET:
Dim number1 as Integer = &H7755
Dim number2 as Integer = &H5577

Dim result as String = Microsoft.VisualBasic.Conversion.Hex(number1 + number2)

Second: You'll have to split and reverse the string yourself, after that you'll have to convert each character to it's character code (Microsoft.VisualBasic.String.Asc()) and use the above function to put it into hex.

Last question is the same:
VB.NET:
Me.TextBox.Text += &H8800000

Bobby
 
Sorry for double posting here, but I haven't managed to get these working on my own, any chance someone could elaborate a little more on what he said so I can get this working?
 
Well, for the hex calculator, the code makes sense, but how would I allow the input for number1 based on input from a user? I've got it working with the sample you give me, but I can't seem to allow it to grab input from a textbox as the value for number1.


For the second example, My main problem is being able to split the string into parts of 4,
I've got a somewhat working version of what I need the code to do, but the problem is that it does not split the text, I need a method of doing that.
txtReverse.Text = StrReverse(TextBox1.Text)

Dim str As String = txtReverse.Text
Dim byteArray() As Byte
Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder

byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str)

For i As Integer = 0 To byteArray.Length - 1
hexNumbers.Append(byteArray(i).ToString("x"))
Next

TextBox2.Text = (hexNumbers.ToString())

And I have not tried the third one yet, but I think once the hex calculator works, I can get that working on my own.
 
If you're not using a NumericUpDown Control you'll have to filter the input which comes into your Textbox by using the PreviewKeyDown Event, checking if it is not a number and set the e.Handled Property to True (this will prevent that the event is passed on).

Or you could do a check with IsNumeric(yourTextBox.Text).

VB.NET:
'input in the textbox is 55CF and FFA5

If IsNumeric(textBox1.Text) and IsNumeric(TextBox2.Text) Then
      Me.resultTextBox.Text = Hex(CInt("&H" & TextBox1.Text) + CInt("&H" & TextBox2.Text))
End If

If the output of the second method should look like this you can do this:
1.) InputString
2.) upnI rtSt gni

VB.NET:
Dim temp as String = TextBox.Text.Length
Dim output as String = ""

For i as Integer = 0 to (temp.Length - 1) / 4 Step 1
      Dim piece as String = temp.Substring(i*4)

      For j as Integer = 3 to 0 Step 1
            output = hex(Asc(piece(j)))
      Next
Next

Without trying it and knowing that at some point this code will crash...but I'm out of ideas right now.

Bobby
 
Back
Top