# Thread: Hex to Decimal conversion (2002/SR1)

1. ## Hex to Decimal conversion (2002/SR1)

I have a database that was created automatically by using Microsofts Application Compatability Analyzer. I use this software to collect PC inventory. It works great and collects all the fields I want to report on but the memory field is displayed in hex for example. My computer has 512mb of memory but it is displaying 0x1ff3a000 in the table. Is there a function to convert this to 512?

Don

2. ## Re: Hex to Decimal conversion (2002/SR1)

You can create a query based on the table, and add a calculated column:

MemoryDecimal: CLng(Replace([MemoryHex],"0x","&H"))

where MemoryHex is the name of the field containing the hexadecimal values, and MemoryDecimal is the name you want to give the calculated column.

3. ## Re: Hex to Decimal conversion (2002/SR1)

To add to HansV's reply, you also need to divide result by 2^20 (1048576) to convert bytes to megabytes:

? CLng(Replace("0x1ff3a000","0x","&H"))
536059904
? 536059904/2^20
511.2265625

Note the result does not round to 512 exactly. The exact number of bytes that equate to 512MB can be calculated in Dec or Hex as follows:

? 512*2^20
536870912

? Hex(512*2^20)
20000000

So the rounding factor would have to be taken into account when displaying this information in megabytes.

HTH

4. ## Re: Hex to Decimal conversion (2002/SR1)

BTW, if you want to "round" a number up or down to nearest multiple of a specified fractional or integer value, you can use this simple function:

<code>Public Function RoundToCustomVal(ByVal dblVal As Double, _</code>
<code> ByVal dblCustomVal As Double) As Double</code>
<code> RoundToCustomVal = Round(dblVal / dblCustomVal, 0) * dblCustomVal</code>
<code>End Function</code>

Example:

<code>? RoundToCustomVal(.17599,.25)</code>
<code> 0.25 </code>

<code>? RoundToCustomVal(31.2999, 32)</code>
<code> 32 </code>

Assuming any installed memory is likely to be in multiples of 32 MB of RAM (ie, 64, 128, 256, etc) you can use this function to "round" the fractional MB value derived from hexadecimal byte count to typical installed RAM value:

<code>? RoundToCustomVal(58.5, 32)</code>
<code> 64 </code>
<code>? RoundToCustomVal(258.0041, 32)</code>
<code> 256 </code>
<code>? RoundToCustomVal(511.2265625, 32)</code>
<code> 512</code>

HTH

5. ## Re: Hex to Decimal conversion (2002/SR1)

Thank you Mark and Hans! This is some very helpful information! Very clear and concise Mark for someone like me that is not very good with number and math! I appreciate the time you spent here very much.

Don

6. ## Re: Hex to Decimal conversion (2002/SR1)

On the same topic...
I've seen code such as the following in a class module:

<pre>Const OFN_ALLOWMULTISELECT As Long = &H200
Const OFN_CREATEPROMPT As Long = &H2000
Const OFN_EXPLORER As Long = &H80000
Const OFN_EXTENSIONDIFFERENT As Long = &H400
Const OFN_FILEMUSTEXIST As Long = &H1000
Const OFN_HIDEREADONLY As Long = &H4
Const OFN_LONGNAMES As Long = &H200000
Const OFN_NOCHANGEDIR As Long = &H8
Const OFN_NODEREFERENCELINKS As Long = &H100000
Const OFN_OVERWRITEPROMPT As Long = &H2
Const OFN_PATHMUSTEXIST As Long = &H800
Const OFN_READONLY As Long = &H1

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String ' Can also be a Long.
End Type

' Private variables.
Private OFN As OPENFILENAME

Private Sub Class_Initialize()
' This routine runs when the object is created.
OFN.Flags = OFN.Flags Or OFN_EXPLORER Or _
End Sub</pre>

What is the purpose of giving a constant a hexadecimal value instead of a decimal one?

7. ## Re: Hex to Decimal conversion (2002/SR1)

The constants could be declared as decimal numbers as well, but since all these numbers are powers of 2, they look "neater" as hexadecimal numbers.

For example, &H80000 looks less confusing than its decimal equivalent 524288.

8. ## Re: Hex to Decimal conversion (2002/SR1)

These declarations provide an interface to one of the Windows API functions. The values to be used are hard-coded into one of the Windows DLLs, so you cannot just use any old value you like - you *must* use the values that have been set by Microsoft's programmers.

The Flags member of the OPENFILENAME type is a Long, i.e. a 32 bit integer. Each of the OFN_... constants corresponds to one bit in the 32 bit integer. For example, OFN_READONLY = &H1 corresponds to the last (least significant) bit, OFN_OVERWRITEPROMPT = &H2 corresponds to the next to last bit etc. This makes it easy to add the constants together. In binary notation (with only 0s and 1s):

OFN_OVERWRITEPROMPT = ...0010

9. ## Re: Hex to Decimal conversion (2002/SR1)

Thank you Hans,
I've taken the previous examples from the attached mdb.

If you place a breakpoint at the line
.Extension = "mdb"
in the ReConnect function in the Modulo1 module and hover the mouse over
.FileName and .Directory in the preceding lines, you get .FileName = "" and .Directory = ""

Why don't .FileName and .Directory acquire the value on the right side of the equals sign?

10. ## Re: Hex to Decimal conversion (2002/SR1)

I don't know, but the code still works, so don't worry about it.

11. ## Re: Hex to Decimal conversion (2002/SR1)

Another thing I don't understand is when I step through code and hover the mouse over the components of the statement
OFN.Flags = OFN.Flags Or nFlags
in the ComDlg module;
before you execute it the values are
OFN.Flags = 2621444 nFlags = 6144
after you execute it
OFN.Flags becomes 2627588
How come the Or operator adds the two values on the right side of the expression?

12. ## Re: Hex to Decimal conversion (2002/SR1)

It follows from what I explained earlier in this thread. It's easier to demonstrate with smaller values.

Let's say that OFN.Flags = 32 and nFlags = 4. In binary notation, writing down only the last 8 digits, OFN.Flags = 00010000 and nFlags = 00000100. The binary Or operator results in a 0 if both arguments have a 0 in that position, 1 otherwise. So OFN.Flags Or nFlags = 00010100 binary = 36 decimal. Since the 1s were in different positions, the binary Or operator has the same result as adding the numbers.

13. ## Re: Hex to Decimal conversion (2002/SR1)

Thank you Hans, one final question if I may. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>
Why is there a crosshatch at the end of a statement such as
Public Const gTHISVERSION As Currency = 2#
?

14. ## Re: Hex to Decimal conversion (2002/SR1)

The # symbol forces the value 2 to be interpreted as a Double precision floating point number. It is not really necessary here because the constant is explicitly declared as a Currency value. But try the following example:

Sub TestTypes()
Const a = 37
Const b = 37#
MsgBox "a is of type " & VarType(a) & vbCrLf & _
"b is of type " & VarType([img]/forums/images/smilies/cool.gif[/img]
End Sub

If you run this procedure, you'll see

a is of type 2
b is of type 5

2 = Integer, 5 = Double (see the help for the VarType function).

15. ## Re: Hex to Decimal conversion (2002/SR1)

Thank you Hans, your replies are always very educational.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•