signed result of Val function in VBA

thed

I use vba in ms access,and found that ,if my parameter greater than 0x8000 less than 0x10000, the result is minus number

eg. Val("&H8000") = -32768 Val("&HFFFF")= -1

how can i get the unsigned number?
thanks!

Mathieu Guindon

There's a problem right here:

?TypeName(&HFFFF)
Integer

The Integer type is 16-bit, and 65,535 overflows it. This is probably overkill, but it was fun to write:

Function ConvertHex(ByVal value As String) As Double
    
    If Left(value, 2) = "&H" Then
        value = Right(value, Len(value) - 2)
    End If
    
    Dim result As Double
    
    Dim i As Integer, j As Integer
    For i = Len(value) To 1 Step -1
        
        Dim digit As String
        digit = Mid$(value, i, 1)
        
        result = result + (16 ^ j) * Val("&H" & digit)
        j = j + 1
        
    Next
    
    ConvertHex = result
    
End Function

This function iterates each digit starting from the right, computing its value and adding it to the result as it moves to the leftmost digit.


?ConvertHex("&H10")
 16 
?ConvertHex("&HFF")
 255 
?ConvertHex("&HFFFF")
 65535 
?ConvertHex("&HFFFFFFFFFFFFF")
 281474976710655 

UPDATE

As I suspected, there's a much better way to do this. Credits to @Jonbot for this one:

Function ConvertHex(ByVal value As String) As Currency
    Dim result As Currency
    result = CCur(value)

    If result < 0 Then
        'Add two times Int32.MaxValue and another 2 for the overflow
        'Because the hex value is apparently parsed as a signed Int64/Int32
        result = result + &H7FFFFFFF + &H7FFFFFFF + 2
    End If

    ConvertHex = result
End Function

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

excel VBA return result of function

From Dev

VBA function result type mismatch

From Dev

Store into variable result of function VBA

From Dev

Number.prototype.function not working on result of jQuery(..).val()

From Dev

Number.prototype.function not working on result of jQuery(..).val()

From Dev

Signed result of subtracted unsigned integers?

From Dev

Unexpected Result when Overriding 'val'

From Dev

Is there existing Documentation about why the VBA Val function behaves differently than .Net implementation of same code (hex conversions)?

From Dev

Pass Excel Range in VBA Function, Process as Array, and Return Result

From Dev

warning of signed/unsigned in function

From Dev

Assign an existing function to a val?

From Dev

Val Function and overload notice

From Dev

TypeError: .val is not a function

From Dev

jQuery val is correct function?

From Dev

option selected with val() function

From Dev

Val Function and overload notice

From Dev

Defining function using val

From Dev

Wrong result after dividing signed integer

From Dev

Wrong result after dividing signed integer

From Dev

jquery comparing string with .val(); non expected result

From Dev

jquery comparing string with .val(); non expected result

From Dev

fiirebase result.val is null with await

From Dev

Kotlin - wrong val result in class with secondary constructor

From Dev

def or val for defining Function in Scala

From Dev

.val() function is trimming special character

From Dev

Function Literal referencing by val and def

From Dev

def or val for defining Function in Scala

From Dev

.val() function is trimming special character

From Dev

jquery val(); function and ruby on rails

Related Related

HotTag

Archive