Вопрос

I have an interesting twos complement problem.

<meta http-equiv="x-ua-compatible" content="IE=10">
<script type="text/vbscript">
     HexVal = "F8" 
     document.write(CLng("&H" & HexVal) & "<br />")
</script>

You can run the above code in IE11 (or earlier?), and you will see the following output:

248

This indicates to me that the CLng function is interpreting the hex value as an unsigned integer when it prints it in decimal form. However, F8 is actually -8 in twos complement. How can I force vbscript to recognize this?

Это было полезно?

Решение

Function TwoComplement8Bits(value)
    TwoComplement8Bits=CInt(value)
    If TwoComplement8Bits>127 Then 
        TwoComplement8Bits=-1*((Not (TwoComplement8Bits Or &hff00))+1)
    End If
End Function

WScript.Echo TwoComplement8Bits("&hf8")
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top