Solved

Hex addition and conversion

Posted on 2004-03-28
8
385 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:scorpio05678
8 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 10698320
'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
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
ID: 10698482
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 ?  :)
0
 
LVL 27

Accepted Solution

by:
Ark earned 200 total points
ID: 10700065
Private Sub Command1_Click()
   Dim i As Integer
   Dim s As String
   For i = 0 To 10
       s = LongToHex(i * 16) & "-" & LongToHex((i + 1) * 16 - 1)
       Debug.Print s
   Next i
End Sub

Private Function LongToHex(ByVal lVal As Long, Optional nDigits As Integer = 4) As String
   Dim sTemp As String
   sTemp = String(nDigits, "0")
   sTemp = Right(sTemp & Hex(lVal), nDigits)
   LongToHex = sTemp
End Function
0
 

Author Comment

by:scorpio05678
ID: 10700580
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
0
 
LVL 2

Expert Comment

by:jasonholland
ID: 10702220
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
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

757 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

22 Experts available now in Live!

Get 1:1 Help Now