Solved

increment hexadecimal number in vbscript

Posted on 2000-04-14
11
1,209 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

624 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