Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

increment hexadecimal number in vbscript

Posted on 2000-04-14
11
Medium Priority
?
1,244 Views
Last Modified: 2008-03-03
This is VB script question, but we have no VB script area
so it is here. It is not webrelated.
Problem:
to increment hexadecimal number in vb script programmatically
(no data types)
for example you have
a = 00 00 00  now you want +1
and you want to go up to
a = ff ff ff ff
is it possible?

0
Comment
Question by:mativare
[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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 1200 total points
ID: 2716624
Dim strA As String

strA = "0000"

Increment:

strA = Right("0000" & Hex(Clng("&H" & strA) + 1), 4)

Decrement:
strA = Right("0000" & Hex(Clng("&H" & strA) - 1), 4)

Et voilá
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2716654
Oh, that's integers. For longs:

strA = "00000000"

Increment:
If strA = "7FFFFFFF" Then
    strA = "80000000"
Else
    strA = Right("00000000" & Hex(Clng("&H" & strA) + 1), 8)
End If

Decrement:
If strA = "80000000" Then
    strA = "7FFFFFFF"
Else
    strA = Right("00000000" & Hex(Clng("&H" & strA) - 1), 8)
End If

The IF statements cater for the fact that longs are signed, and the range "00000000" to "FFFFFFFF" is not.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2716674
Public Function IncrementedHex(ByVal N)
Dim lngLoop
Dim blnOverflow
Dim Digit

blnOverflow = True
lngLoop = Len(N)
While blnOverflow
  blnOverflow = False
  If lngLoop = 0 Then
    N = "1" & N
  Else
    Digit = Mid(N, lngLoop, 1)
    If Digit = "F" Then
      Mid(N, lngLoop, 1) = "0"
      blnOverflow = True
    Else
      If IsNumeric(Digit) Then
        If Digit = "9" Then
          Mid(N, lngLoop, 1) = "A"
        Else
          Mid(N, lngLoop, 1) = Digit + 1
        End If
      Else
        Mid(N, lngLoop, 1) = Chr(Asc(Digit) + 1)
      End If
    End If
  End If
  lngLoop = lngLoop - 1
Wend

IncrementedHex = N
End Function
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Expert Comment

by:Erick37
ID: 2716685
'Increments a 4 byte hex number represented as a string
Function IncHex(ByVal sHex As String)
    Dim n1 As Long, n2 As Long
    'n1 and n2 are the lo and hi order integers
    n1 = CLng("&H" & Right(sHex, 4))
    n2 = CLng("&H" & Left(sHex, 4))
    'Add one
    n1 = n1 + 1
    'Check for rollover 0000FFFF -> 00010000
    If n1 > 65535 Then
        n1 = 0
        n2 = n2 + 1
        'Check for rollover FFFFFFFF -> 00000000
        If n2 > 65535 Then n2 = 0
    End If
    IncHex = String(4 - Len(Hex(n2)), "0") & Hex(n2) & _
        String(4 - Len(Hex(n1)), "0") & Hex(n1)
End Function
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2716717
PS - you'll need to implement similar if statements for 7FFF and 8000 for Integers.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2716725
..and use CInt instead of CLng

Sorry for the multiple posts.
0
 
LVL 1

Author Comment

by:mativare
ID: 2717172
Adjusted points from 100 to 300
0
 
LVL 1

Author Comment

by:mativare
ID: 2717173
So this is possible?
you are  good
some minor things
Please notice spaces in variant
spaces are mission critical and
must be present at any time
the actual format is
00 00 00 00
it means Len must be 11
at any time
and position 3,6,9 must be empty
not even 0
I "incremented" points to 300

0
 
LVL 32

Expert Comment

by:Erick37
ID: 2717230
'Increments a 4 byte hex number represented as a string
''Format of Hex - "00 00 00 00"
Function IncHex(ByVal sHex As String)
    Dim n1 As Integer, n2 As Integer, n3 As Integer, n4 As Integer
    'n1 - n4 are the lo to hi bytes
    n1 = CInt("&H" & Mid(sHex, 10, 2))
    n2 = CInt("&H" & Mid(sHex, 7, 2))
    n3 = CInt("&H" & Mid(sHex, 4, 2))
    n4 = CInt("&H" & Mid(sHex, 1, 2))
   
    'Add one
    n1 = n1 + 1
    If n1 > 255 Then
        n1 = 0
        n2 = n2 + 1
        If n2 > 255 Then
            n2 = 0
            n3 = n3 + 1
            If n3 > 255 Then
                n3 = 0
                n4 = n4 + 1
                If n4 > 255 Then n4 = 0
            End If
        End If
    End If
    IncHex = Right("00" & Hex(n4), 2) & " " & _
                Right("00" & Hex(n3), 2) & " " & _
                Right("00" & Hex(n2), 2) & " " & _
                Right("00" & Hex(n1), 2)
End Function
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2717276
strA = "00 00 00 00"

Increment:
If strA = "7F FF FF FF" Then
    strA = "80 00 00 00"
Else
    strA = Left(strA, 2) & Mid(strA, 4, 2) & Mid(strA, 7, 2) & Right(strA, 2)
    strA = Right("00000000" & Hex(Clng("&H" & strA)  + 1), 8)
    strA = Left(strA, 2) & " " & Mid(strA, 3, 2) & " " & Mid(strA, 5, 2) & " " & Right(strA, 2)

End If

I'm sure you can work out Decrement :-)
0
 
LVL 1

Author Comment

by:mativare
ID: 2717891
Thank you
It functions and is short
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

704 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