Avatar of scorpio05678
scorpio05678Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Hex addition and conversion

HI

I am writing an app which sends message to server/client and display them in the rich text box. All the messages send and recieved are displyed in Hex . Each line should display 16 hex pairs with their starting and end index(in 4 digit hex). I can display the starting and ending index in decimal but conversion to  Hex cause apps to display error

Current
********************
Text: "This is a test for displaying index in decimal and it appears to be working"

Hex:
0000-0015  54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 66    
0016-0031  6F 72 20 64 69 73 70 6C 61 79 69 6E 67 20 69 6E    
0032-0047  64 65 78 20 69 6E 20 64 65 63 69 6D 61 6C 20 61    
0048-0063  6E 64 20 69 74 20 61 70 70 65 61 72 73 20 74 6F    
0064-0073  20 62 65 20 77 6F 72 6B 69 6E 67  

**********************

Should be
**********************
0000-000F  54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 66    
0010-001F  6F 72 20 64 69 73 70 6C 61 79 69 6E 67 20 69 6E    
0020-002F  64 65 78 20 69 6E 20 64 65 63 69 6D 61 6C 20 61    
0030-003F  6E 64 20 69 74 20 61 70 70 65 61 72 73 20 74 6F    
0040-004B  20 62 65 20 77 6F 72 6B 69 6E 67  
**********************                


anyone have an idea how to do Hex addition and decimal to Hex (4 digit ) conversion.

Thanks

SC
Visual Basic Classic

Avatar of undefined
Last Comment
jasonholland
Avatar of Shauli
Shauli

'In tthe function below, fnDecToAdd is the number you want to add (15 in your case) and fnMyDec is the base number to which you want to add. All calculations are done in decimal and then converted to Hex. Finally, it adds as many zeros as needed:

Private Sub Command1_Click()
MsgBox ConvertAddToHex(32, 15) 'execute. In this sample the result would be 002F
End Sub

Private Function ConvertAddToHex(ByVal fnMyDec As Long, fnDecToAdd As Long) As String
Dim x As String, y As String
x = Hex(fnMyDec + fnDecToAdd) 'calculate and convert to Hex
y = Len(x)
ConvertAddToHex = String(4 - y, "0") & x 'add zeros up to four places
End Function

S
Look this ! :)

To Add decimal value 52 to HEX value 1B use this:

Dim strHex As String
Dim intDec As Integer
Dim strResult As String
dim lngResult As Long

strHex="1B"
intDec=52

lngResult=VAL("&H" & strHEX)+intDec

MsgBox Hex$(lngResult)

Easy, isn't it ?  :)
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of scorpio05678
scorpio05678
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

hi guys

Sorry guys my index counting mechanism is messed up. I cann't even count the index in decimal properly and below is the code.(some times it displays 2 less on the last line and sometime not and also on boundary condition  (i.e. when there are 17 hex pair) first line is ok (0000-000F) but on 2nd line it displays 0010-0011 instead of  0010-0010.

This method takes a line of hex and chop it into line of 16 hex pair(32) and 16 spaces(i.e. allowedHexLength = 48 ) and store it into temprory UDT. Then calculate the starting and ending  Index and store at appropriate place in Array of UDT. I have used "hexBytes" to count the number of pair on 1 line when length of line is less then 48 so that i can add it to find the ending index for that line.

I need to store all this info in Array of UDT for my other method to just display it on the screen when and if needed.

******************************
   Private Type hexMessages
         hexLine As String
        startIndex As String
        endIndex As String
    End Type

    Private hexMessageArray() As hexMessages

    Dim thisLength As Integer
    Dim thisIndex As Double
    Dim thisStartIndex As Integer
    Dim thisEndIndex As Integer
         
    'calculate the length of line
    thisLength = Len(thisHexLine)
    'create appropriate size buffer for each line(Array of UDT)
    If thisLength > allowedHexLength Then
       ReDim hexMessageArray(thisLength \ allowedHexLength)
    Else
        ReDim hexMessageArray(0)
    End If
    'start putting the maximum allowed hex line in the buffer
    For thisIndex = LBound(hexMessageArray) To UBound(hexMessageArray)
        For intPlace = 1 To allowedHexLength
            strChar = Mid$(thisHexLine, intPlace, 1) 'Get 1 chars at a time
            If strChar = "" Then 'means end of line
                hexBytes = hexBytes + 2
                Exit For
            End If
            If strChar = " " Then ' means 1 pair is done ,increase the pair counter and also add space
                hexMessageArray(thisIndex).hexLine = hexMessageArray(thisIndex).hexLine + " "
                hexBytes = hexBytes + 1
            Else
                hexMessageArray(thisIndex).hexLine = hexMessageArray(thisIndex).hexLine _  
                                                                         + strChar  'Add it to the current word
            End If
        Next
       'calculate the starting and ending index
       If thisIndex = 0 Then ' for 1st loop only coz starting it from 0
            hexMessageArray(thisIndex).startIndex = "0"
            hexMessageArray(thisIndex).endIndex = Val((hexMessageArray(thisIndex).startIndex)  _
                                                                      + (hexBytes - 1))                      
        Else
            hexMessageArray(thisIndex).startIndex = Val((hexMessageArray(thisIndex  _
                                                                       -   1).endIndex) + 1)
            hexMessageArray(thisIndex).endIndex = Val((hexMessageArray(thisIndex).startIndex) _
                                                                       + (hexBytes - 1))            
        End If
Next

******************************

I know it is any extra work so i am increasing 25 pts. Sorry for any typos coz i am tooo tired to type

thanks

SC
Avatar of jasonholland
jasonholland

If you don't want to do all of the conversion's youself you can use my DLL.

http://www.rawsoftware.com/list.asp?list=Software&License=All

Download cHex.zip, its in the list on that page. To see how to use it read the Full Story. It will
explain everything you should need to know.

Jason Holland
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo