Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...Thanks a lot Mate! I can't tell you how many times your site has saved my "rear". hehe..."

Geography

Where in the world do Tek-Tips members come from?

Converting a decimal number into a hexadecimal valueHelpful Member!(2) 

robojeff (TechnicalUser)
10 Jul 12 9:56

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
SkipVought (Programmer)
10 Jul 12 10:05
Hex(YourNumber)

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

MichaelRed (Programmer)
10 Jul 12 11:35
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


SkipVought (Programmer)
10 Jul 12 12:21
I think that the OP meant BASE 10 rather than DECIMAL.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

robojeff (TechnicalUser)
13 Jul 12 7:54


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
SkipVought (Programmer)
13 Jul 12 8:15

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

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Helpful Member!  PHV (MIS)
13 Jul 12 8:31
No need of the Len function:
Right("0000" & Hex(YourNumber), 4) & "h"

Hope This Helps, PH.
FAQ219-2884: How Do I Get Great Answers To my Tek-Tips Questions?
FAQ181-2886: How can I maximize my chances of getting an answer?

robojeff (TechnicalUser)
13 Jul 12 10:15

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
robojeff (TechnicalUser)
13 Jul 12 14:24

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!

robojeff (TechnicalUser)
16 Jul 12 9:50

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
Helpful Member!  SkipVought (Programmer)
16 Jul 12 10:07

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,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

robojeff (TechnicalUser)
16 Jul 12 10:38

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
              indx = Mid(k, loop1, 1)
               indxk = Val("&H" & indxk)
               totk = totk + (indxk * Hx(loop1))
            Next 
SkipVought (Programmer)
16 Jul 12 10:46

CODE

For loop1 = 0 To 3
       indx = Mid(k, loop1 + 1, 1)
       indxk = Val("&H" & indxk)
       totk = totk + (indxk * Hx(loop1))
   Next 

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

robojeff (TechnicalUser)
16 Jul 12 10:48

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!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close