Solved

VB.Net adding and substracting 2 Hexadecimal values

Posted on 2013-06-03
7
279 Views
Last Modified: 2013-06-05
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 :(

Can you please help? My code is below.

    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
        Dim resultado As String

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

        If cboOperacion.Text = "+" Then
            resultado = first_hex_value + second_hex_value
        Else
            resultado = first_hex_value - second_hex_value
        End If

        txtResultado.Text = resultado
    End Sub

Open in new window

0
Comment
Question by:José Perez
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39216363
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

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 39220418
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

        txtResultado.Text = hexResult.ToUpper
        txtValor_Hexa.Text = hexResult.ToUpper

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39220575
What is the error?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:José Perez
ID: 39221884
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'.

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 39222250
Edited, Added English translation for error, see above.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 39222386
Change:

Dim result As String

to:

Dim result As Integer
0
 
LVL 2

Author Comment

by:José Perez
ID: 39222504
Perfect!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question