[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.Net adding and substracting 2 Hexadecimal values

Posted on 2013-06-03
7
Medium Priority
?
329 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

872 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