[Webinar] Streamline your web hosting managementRegister Today

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 346

Hi colleagues,
I am trying to create a Hexadecimal calculator that allows me to add and substract 2 values but I am failing to do it :(

``````    Private Sub btnCalcular_Click(sender As Object, e As EventArgs) Handles btnCalcular.Click
Dim first_hex_value As Byte
Dim second_hex_value As Byte
Dim operacion As String

first_hex_value = txt1erValor.Text '10
second_hex_value = txt2doValor.Text '1

If cboOperacion.Text = "+" Then
Else
End If

End Sub
``````
0
José Perez
• 4
• 3
1 Solution

Commented:
You can use the overloads of the Convert class to convert hex values to decimal values, then do the arithmetic, then spit the answer back as hex using another overload of the Convert class:

``````Module Module1

Sub Main()
Dim hex1 As String = "10"
Dim hex2 As String = "1"
Dim int1 As Integer = Convert.ToInt32(hex1, 16)
Dim int2 As Integer = Convert.ToInt32(hex2, 16)
Dim result As Integer = (int1 + int2)
Dim hexResult As String = Convert.ToString(result, 16)

Console.WriteLine(hexResult)

End Sub

End Module
``````
0

Author Commented:
I updated my code with your code and it is displaying an error, see my code please:

``````        Dim hex1 As String = txt1erValor.Text 'this is the first value
Dim hex2 As String = txt2doValor.Text 'this is the second value
Dim int1 As Integer = Convert.ToInt32(hex1, 16)
Dim int2 As Integer = Convert.ToInt32(hex2, 16)
Dim result As String

If cboOperacion.Text = "+" Then
result = (int1 + int2)
ElseIf cboOperacion.Text = "-" Then
result = (int1 - int2)
End If

Dim hexResult As String
hexResult = Convert.ToString(result, 16) ' the error is displayed here

txtValor_Hexa.Text = hexResult.ToUpper
``````
0

Commented:
What is the error?
0

Author Commented:
In English it says "Error 1 Error of overload resolution because none of 'ToString' functions" can be called without a restrict conversion"

``````Error	1	Error de resolución de sobrecarga porque ninguna de las funciones 'ToString' a las que se tiene acceso se puede llamar sin una conversión de restricción:

'Public Shared Function ToString(value As Long, toBase As Integer) As String': El parámetro 'value' correspondiente al argumento se reduce de 'String' a 'Long'.

'Public Shared Function ToString(value As Integer, toBase As Integer) As String': El parámetro 'value' correspondiente al argumento se reduce de 'String' a 'Integer'.

'Public Shared Function ToString(value As Short, toBase As Integer) As String': El parámetro 'value' correspondiente al argumento se reduce de 'String' a 'Short'.

'Public Shared Function ToString(value As Byte, toBase As Integer) As String': El parámetro 'value' correspondiente al argumento se reduce de 'String' a 'Byte'.
``````
0

Author Commented:
Edited, Added English translation for error, see above.
0

Commented:
Change:

Dim result As String

to:

Dim result As Integer
0

Author Commented:
Perfect!
0

Featured Post

• 4
• 3
Tackle projects and never again get stuck behind a technical roadblock.