Jump to content

Signed and unsigned


Recommended Posts

I´m using a DLL and Autoit Library (I create the library, BB) that acess this DLL and on my program a receive a signed variable from the BB library, but sometimes I want use this value as signed or unsigned, how I declare a unsigned variable on Autoit?

Regards,

Brunomusw

Link to comment
Share on other sites

Thanks Brett, I´ve read this already and it not help.

Let´s example this.

I have a return value from a function, that´s a signed char, but sometimes I need to interpret it as unsigned char.

Reading "Language Reference" there is no way to do it.

Regards,

Brunomusw

Edited by brunomusw
Link to comment
Share on other sites

Look into the Abs() function and using the - symbol to switch to signed (or reverse the sign)

$iVal=Abs($iVal) ; Remove any sign

$iVal= -$iVal ; Make negative or positive (depending on initial value)

$iVal *= -1 ; also does the trick..

One thing you need to know - Anything with the 63rd bit set will become negative. Also, anything coming out of a Bit*() operation with the 31st bit set, or returns from DLLCall()'s with *signed* values will be sign-extended.

*edit: I should note that, when I talk about the 'nth bit set, I'm talking from a base index of 0, so the 63rd bit is really the top bit of a 64-bit #.

Edited by Ascend4nt
Link to comment
Share on other sites

Thanks guys...

But I the way that I see is by calculus.

Once the computer uses 2´s complements and I´m receving a byte, when I want that it be an unsigned I will need to + 256.

func() returns -100.

Reading it as unsigned it will be = -100 + 256 = 156.

Regards,

Brunomusw

Link to comment
Share on other sites

You're not making a whole lotta sense because you aren't providing any code demonstrating how you are getting the data or interpreting it. A DLLCall allows unsigned values to be returned you know, like 'ubyte'..

However, just for kicks - if you are talking about simply converting a negative 1-byte value to its binary positive equivalent (not its positive absolute value), you can do this:

$iChar=-5
$uChar=Number(BinaryMid($iChar,1,1))
MsgBox(0,"Transforming the sign bit","Original value:"&$iChar&@CRLF&"Value reconsidered without a sign bit:"&$uChar&@CRLF)

Note that the above ASSUMES you are using just one character (signed value maximum range: -128 to 127). To do words, dwords, or qwords you'll need a different approach..

Edited by Ascend4nt
Link to comment
Share on other sites

You're not making a whole lotta sense because you aren't providing any code demonstrating how you are getting the data or interpreting it. A DLLCall allows unsigned values to be returned you know, like 'ubyte'..

However, just for kicks - if you are talking about simply converting a negative 1-byte value to its binary positive equivalent (not its positive absolute value), you can do this:

$iChar=-5
$uChar=Number(BinaryMid($iChar,1,1))
MsgBox(0,"Transforming the sign bit","Original value:"&$iChar&@CRLF&"Value reconsidered without a sign bit:"&$uChar&@CRLF)

Note that the above ASSUMES you are using just one character (signed value maximum range: -128 to 127). To do words, dwords, or qwords you'll need a different approach..

That's what I mean, your code is another solution, but the on the sameway that doing calculus it's a trick.

Actually We did it on the DLL, and just to add we found some bug over there.

If $sign = 0 Then           ;unsigned
  $ret = DllCall($dll, "byte", "ReadByteComP")
 ElseIf $sign = 1 Then      ;signed
  $ret = DllCall($dll, "int", "ReadByteComP")   
 EndIf

Using the ubyte and byte both are giving unsigned byte. How to report it?

Regards,

Brunomusw

Link to comment
Share on other sites

Hmm, your code doesn't make sense. Why are you calling the same function returning a byte and an integer? And I don't see any 'ubyte' or 'uint' return types there.

Still, now you're telling me that its always returning an unsigned value?? I thought the original problem was that it was giving you a signed value that you wanted to convert to unsigned. :)

If you have access to the DLL code - look closely at how the value is being returned. If it's a byte that's being returned, the DLL code should either sign-extend or zero-extend that value to 32-bits (or 64 in x64 mode) if you're trying to grab it as an int.

Otherwise, 'byte' should return signed when the upper bit is set (a value >127 positive will set the upper bit, and of course if it's treated as a signed value, any negative # will have that upper bit set).

For a quick example (this isn't a DLLCall, but it shows what 'byte' returns when set with a value with the upper bit set):

$stStruct=DllStructCreate("byte")
DllStructSetData($stStruct,1,129)
MsgBox(0,"DLLStruct test","Value 129 read back from DLL Structure:"&DllStructGetData($stStruct,1))

I think you need to further explore this before reporting it as a bug. To report it, it should be something the AutoIT developers can replicate themselves. If you have a special DLL that isn't standard then you won't be likely to get support unless you also provide the DLL code (at least the relevant parts).

If you still think you have a legitimate bug, and can have it replicated by the AutoIT developers, then by all means click on 'Bug Reports and Feature Requests' on the main Forum Index. (be sure to read the notes there 1st)

Link to comment
Share on other sites

Thanks guys...

But I the way that I see is by calculus.

Once the computer uses 2´s complements and I´m receving a byte, when I want that it be an unsigned I will need to + 256.

func() returns -100.

Reading it as unsigned it will be = -100 + 256 = 156.

Regards,

Brunomusw

I think you need to create 2 structs in the same area of memory, or a union.

$stB = dllstructcreate("byte")

$stU = dllstructcreate("ubyte",dllstructgetptr($stB)

When you get the return byte from your dll save it in $StB. DllStructSetDat($stB,1,$result)

If you want to read it as an unsigned byte read it from $stU. DllStructGetData($stU,1)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I think you need to create 2 structs in the same area of memory, or a union.

$stB = dllstructcreate("byte")

$stU = dllstructcreate("ubyte",dllstructgetptr($stB)

When you get the return byte from your dll save it in $StB. DllStructSetDat($stB,1,$result)

If you want to read it as an unsigned byte read it from $stU. DllStructGetData($stU,1)

Thanks Martin, it´s worked.

But, why using the DllCall didn´t work?

Regards,

Brunomusw

Link to comment
Share on other sites

Sorry Ascend4nt's, that's what I was looking for, is like the Martin's code.

Create an Struct for signed and unsigned to receive the values from the DLL.

But why the DLLCall didn't work?

$ret = DllCall($dll, "byte", "ReadByteComP")
$ret = DllCall($dll, "ubyte", "ReadByteComP")

Regards,

Brunomusw

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...