Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi everybody,
I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Tue, 8 May 2007 09:52:02 -0700, Alex wrote:
Hi everybody, I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex I believe this occurs because hex numbers with four or fewer digits are converted to signed 2-byte integers. When the signed integer intermediate value is converted to a Long (or 4-byte) integer, the sign is propagated. --ron |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() &hFF processes correctly. I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check lvalue ?! But I guess this question not for you. :) Thank you. "Ron Rosenfeld" wrote: On Tue, 8 May 2007 09:52:02 -0700, Alex wrote: Hi everybody, I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex I believe this occurs because hex numbers with four or fewer digits are converted to signed 2-byte integers. When the signed integer intermediate value is converted to a Long (or 4-byte) integer, the sign is propagated. --ron |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
But I guess this question not for you. :)
Why that - Ron gave you a good concise answer !! I merely followed to show how to set the Hex to a Long. As Ron said, 4 digit Hex values are Integers unless explicitly set to a Long with the trailing & Integers range from -32768 to +32767, so - &H8000 + &H7FFF = &HFFFF = -1 If you assign &HFFFF to a Long variable the sign is propagated and coerced into the new Long value of - Debug.Print Hex(CLng(&HFFFF)) ' &HFFFFFFFF Regards, Peter T "Alex" wrote in message ... &hFF processes correctly. I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check lvalue ?! But I guess this question not for you. :) Thank you. "Ron Rosenfeld" wrote: On Tue, 8 May 2007 09:52:02 -0700, Alex wrote: Hi everybody, I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex I believe this occurs because hex numbers with four or fewer digits are converted to signed 2-byte integers. When the signed integer intermediate value is converted to a Long (or 4-byte) integer, the sign is propagated. --ron |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Tue, 8 May 2007 10:41:01 -0700, Alex wrote:
&hFF processes correctly. I just wonder why compiler doesn't allow to use &h00FFFF if it doesn't check lvalue ?! But I guess this question not for you. :) Thank you. Your initial theory was that there is a "bug with long integers in VBA" As I explained to you, the problem is not that, but rather the problem is your understanding of how VBA treats HEX numbers, and also with data conversions. For example, a "minor" change in your routine prevents the hexadecimal values from being treated as signed two-bit integers: ======================== Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF& Dim lVal As Long lVal = &HFFFF& Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub ============================= The immediate window shows: lVal: 65535 FFFF Equal --ron |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Try this -
Debug.Print &HFFFF, &HFFFF& ' -1 65535 Regards, Peter T "Alex" wrote in message ... Hi everybody, I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thank you!
"Peter T" wrote: Try this - Debug.Print &HFFFF, &HFFFF& ' -1 65535 Regards, Peter T "Alex" wrote in message ... Hi everybody, I tried assign values using hexidecimal notation in Excel 2003 SP2 and found that &hFFFF is not equal to &h10000 - 1 for LONG integers. Sample is he Sub test() Const WorkaroundValue As Long = &H10000 - 1 Const WrongValue As Long = &HFFFF Dim lVal As Long lVal = &HFFFF Debug.Print "lVal: " & lVal & " " & Hex(lVal) If WorkaroundValue = WrongValue Then Debug.Print "Equal" Else Debug.Print "Not Equal: " & WorkaroundValue & "," & WrongValue Debug.Print "In hex: " & Hex(WorkaroundValue) & "," & Hex(WrongValue) End If End Sub Cheers, Alex |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
http://CannotDeleteFile.net - Cannot Delete File? Try Long Path ToolFilename is too long? Computer Complaining Your Filename Is Too Long? TheLong Path Tool Can Help While most people can go about their businessblissfully unaware of the Windo | Excel Discussion (Misc queries) | |||
Long Long Long Nested If Function | Excel Discussion (Misc queries) | |||
Clearing cells takes long, long time | Excel Discussion (Misc queries) | |||
A list of Consecutive Integers, can I search for missing integers | Excel Worksheet Functions | |||
converting to Integers | Excel Worksheet Functions |