Solved

increment hexadecimal number in vbscript

Posted on 2000-04-14
11
1,141 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 300 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 142

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

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

16 Experts available now in Live!

Get 1:1 Help Now