Solved

increment hexadecimal number in vbscript

Posted on 2000-04-14
11
1,150 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 50
Vb.net 2008 2 56
Problem to skip loop 6 55
Adding to a VBA? 6 61
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…
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…
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…
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…

911 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

24 Experts available now in Live!

Get 1:1 Help Now