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

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.