Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a decimal number into a hexadecimal value 2

Status
Not open for further replies.

robojeff

Technical User
Dec 5, 2008
220
0
0
US

Is there an easy way to convert a decimal number into a hexadecimal value?

I tried the following but I think it is a hexadecimal to decimal conversion because when my number = 10, it is converted to 16:

num = CInt("&H" & num)

Any ideas on this are appreciated...

thanks
 
Hex(YourNumber)

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
HeX to decimal. NO. Although you may regard the resultant as desired, hex only represents "whole" numbers, thus there will never be a decimal part



MichaelRed


 
I think that the OP meant BASE 10 rather than DECIMAL.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


I like Hex(YourNumber)

Is there a way to force this to be a 4 digit number where the result would be 000Ah instead of Ah
or 1FFh = 01FFh?

Thanks
 

This is STRING minipulation. So use LEN and RIGHT to return the length you want.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
No need of the Len function:
Right("0000" & Hex(YourNumber), 4) & "h"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

How about reading in a string that contains a hexidecimal number?

I have the following code where I am parsing the right most four characters which is
a Hexadecimal number.

I then attempt to convert this to a val so that I can perform some math on this
number before converting it back into a string value.

The val statement seems to work unless I encounter a hexadecimal number that
contains the characters not found in a base 10 number set (A, B,C,D,E, & F)
where then I get a resultant of 0.

Code:
k = Right(!LMAC, 4)         ' Get the right most 4 digits of the ending MAC address for the device
k = Val(k)                  ' convert this to a number to be incremented later


Is there an easy way to read these four characters as a hexadecimal number and then use this for calculations?


Thanks
 
k = Val("&H" & Right(!LMAC, 4))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

Thanks PHV-

k = Val("&H" & Right(!LMAC, 4))

I tried this with the value of FF9B for LMAC and this gave me a value of -100 when I would have expected this to be 65435...

This makes me wonder if it is calculating this as a signed value instead of an absolute value.

What can be done to get this to come out as an unsigned number?

Thanks again!

 

So I am trying to overcome this limitation with the val command when dealing with hexadecimal numbers greater than 8000h as this command treats the number conversion as if it is a signed value... (i.e. the range of 0h to 7FFFh conversion equals 0 to 4095 & 8000h to FFFFh conversion equals -32768 to -1)

I have put together the following code that I wish to test but it doesn't like my syntax for the declaration of my numeric conversion string Dim Hx() As Integer = {1, 16, 256, 4096}

Dim k, totk, loop1,
Dim Hx() As Integer = {1, 16, 256, 4096}

totk = 0
k = Right(!LMAC, 4)

For loop1 = 0 To 3
totk = totk + (Mid(k, loop1, 1) * Hx(loop1))
Next

What am I doing wrong with the syntax of the numeric array hx() ?

thanks
 

Code:
    Dim k, totk, loop1
    Dim Hx(3) As Integer
    
    For loop1 = 0 To 3
        Hx(loop1) = 16 ^ loop1
    Next
or
Code:
    Dim k, totk, loop1
    Dim Hx
    
    Hx = Array(1, 16, 256, 4096)

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

Thanks Skip-

That gets me past the syntax error but I am encountering another error later in my code
as I am not able to extract each digit of the 4 digit string with the line:
indx = Mid(k, loop1, 1) as it returns a run-time error code 5 Invalid procedure call or argument


Code:
Dim k, totk, loop1, indx 
Dim Hx
Hx = Array(1, 16, 256, 4096) 

totk = 0 
k = Right(!LMAC, 4) 

For loop1 = 0 To 3
             [COLOR=red] indx = Mid(k, loop1, 1)[/color]
               indxk = Val("&H" & indxk)
               totk = totk + (indxk * Hx(loop1))
            Next
 
Code:
   For loop1 = 0 To 3[b]
       indx = Mid(k, loop1[red] + 1[/red], 1)[/b]
       indxk = Val("&H" & indxk)
       totk = totk + (indxk * Hx(loop1))
   Next

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 

Thanks Skip-

I just saw that before I got your reply and I also realized that I have to turn the values in my hx array around so that it will calculate the value correctly.

thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top