?
Solved

VB.Net adding and substracting 2 Hexadecimal values

Posted on 2013-06-03
7
Medium Priority
?
312 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

649 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