In VB6, how would I subtract 1 from a Hex?

Do I need to convert to Integer first?
(have tried HextoDec function, but not having success)

Example
Hex = FF FF
Hex - 1 = FF FE

Dani AI

Generated

Short answer: and are right — for simple literals you can convert to a numeric type, subtract 1 and convert back. That is the easiest route for short values. When the input is a textual block of bytes (for example "FF FF"), when you must preserve fixed byte-width, or when the hex can be longer than VB6 numeric types support, do a byte-wise subtract-with-borrow. That avoids overflow and keeps the exact byte formatting.

Here is a compact VB6 routine that accepts a hex string (spaces or other non-hex characters are ignored), treats the left-most pair as the most-significant byte (MSB), subtracts 1 with borrow across bytes, and returns space-separated two-digit hex bytes. It wraps by default (00 00 -> FF FF); set wrap = False to treat underflow as an error (returns empty string).

Function DecrementHex(hexStr As String, Optional wrap As Boolean = True) As String
    Dim i As Long, ch As String, clean As String
    clean = ""
    For i = 1 To Len(hexStr)
        ch = Mid$(hexStr, i, 1)
        If InStr("0123456789ABCDEFabcdef", ch) > 0 Then clean = clean & ch
    Next
    If Len(clean) = 0 Then Exit Function
    If Len(clean) Mod 2 = 1 Then clean = "0" & clean

    Dim nb As Long: nb = Len(clean) \ 2
    Dim bytes() As Integer: ReDim bytes(1 To nb)
    For i = 1 To nb
        bytes(i) = Val("&H" & Mid$(clean, (i - 1) * 2 + 1, 2))
    Next

    Dim borrow As Integer: borrow = 1
    For i = nb To 1 Step -1
        If bytes(i) >= borrow Then
            bytes(i) = bytes(i) - borrow
            borrow = 0
            Exit For
        Else
            bytes(i) = bytes(i) + 256 - borrow
            borrow = 1
        End If
    Next

    If borrow = 1 Then
        If Not wrap Then Exit Function
    End If

    Dim out As String: out = ""
    For i = 1 To nb
        out = out & Right$("0" & Hex$(bytes(i)), 2)
        If i < nb Then out = out & " "
    Next
    DecrementHex = UCase$(out)
End Function

Notes and troubleshooting:

  • If your data is little-endian, reverse the byte order before/after the call.
  • For single short hex literals the &H/Val/Hex approach mentioned earlier is simpler.
  • Watch VB6 numeric limits (Long is signed 32-bit) — for large hex values use the byte-wise method above.
  • If input contains unexpected characters, strip them first (the function ignores non-hex chars).

Recommended Answers

All 2 Replies

It's not necessary to convert the value to a decimal, cause the system interprets a variety of representation like a number.

Try this code to check:

Dim nValor As Long
'To take hex representation in VB, follow number by an &H sign
nValor = &HFF
MsgBox nValor
nValor = nValor - 1
MsgBox  nValor

I've tried and it works perfectly.
If you have to show in HEX format again, you'll need to convert the format you desire, but think the number is ALWAYS the same - only the representation of it is changing...

MsgBox Hex(nValor)

By the way, you can represent it in many other forms, such as integer, floating point, hexadecimals, binaries, but in VB the variable is always the same thing: a number. You may take care only with precisions for a variable - certain types are more accurately, certain types are less - it means if you put a really greater value like 80000 in an integer type variable, you may get overflow error...


Hope this helps.
Sidnei

In VB6, how would I subtract 1 from a Hex?

Do I need to convert to Integer first?
(have tried HextoDec function, but not having success)

Example
Hex = FF FF
Hex - 1 = FF FE

In VB6, how would I subtract 1 from a Hex?

Do I need to convert to Integer first?
(have tried HextoDec function, but not having success)

Example
Hex = FF FF
Hex - 1 = FF FE

This site may help you. There is more than one way to skin a cat.:-/
It may give you some ideas.
http://www.vb6.us/tutorials/vb6-number-system-functions-hex-oct-etc

I think you can use this to solve your problem by following the instructions you can convert the hex to decimal then subtract one then reverse the process to display the new hex. Yes the hex value would be FF FE

If you scroll down the page you will find
Converting a Hex or Octal Number to Decimal

To convert a hexadecimal or octal number to a decimal value, you must treat the hex or octal value as a string, append "&H" or "&O" in front of the value, and use a conversion function such as Val or CLng to convert the string to a numeric value.

Example:

Dim strMyHexNum As String
Dim lngDecimalValue As Long

strMyHexNum = InputBox("Enter a Hex Value:")
strMyHexNum = "&H" & strMyHexNum
lngDecimalValue = Val(strMyHexNum)

PRINT strMyHexNum; " in hex is equivalent to "; lngDecimalValue; " in decimal."

If the user entered "2AF" as input, the result would be:
2AF in hex is equivalent to 687 in decimal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.