Solved

VB.Net adding and substracting 2 Hexadecimal values

Posted on 2013-06-03
7
266 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 74

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 74

Expert Comment

by:käµfm³d 👽
ID: 39220575
What is the error?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now