Solved

increment hexadecimal number in vbscript

Posted on 2000-04-14
11
1,173 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 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

821 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