Solved

VB.Net adding and substracting 2 Hexadecimal values

Posted on 2013-06-03
7
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

726 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