AutoHotkey Community

It is currently May 26th, 2012, 11:22 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: November 17th, 2009, 6:15 am 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
When using Numget inside the function CheckBytes, it returns mostly wrong values.
The following testscript fills all bytes in bitList with initValue, but Numget reads some other values.
The GetIntAt() function reads always the correct initValue though.
Is there a way to get it right with NumGet in a function?
Code:
   showEveryByte := true  ; false avoids checking every byte by user
   byteCount := 64  ,  initValue := 123
   GrantedCapacity := VarSetCapacity(bitList, byteCount, initValue)
   CheckBytes(&bitList, GrantedCapacity)      ; test for NumGet in function

   ; If not in a function, NumGet never causes a problem:
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(&bitList, byteNumber, "char")
      intByte := GetIntAt(&bitList, byteNumber, 1)
      if (showEveryByte  ||  numByte <> initValue  ||  intByte <> initValue){
         msgbox, 1, Outside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%)
         IfMsgBox, Cancel
            break         
      }
   }
   VarSetCapacity(bitList, 0)    ; free bitList
   ExitApp
F9::ExitApp
return

CheckBytes(pBitList, GrantedCapacity){
   ; W/ NumGet in a function, it causes always a problem at some byte(s)
   global showEveryByte, initValue
   
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(pBitList, byteNumber, "char")   ; pBitList or *pBitList does NOT work for me
      intByte := GetIntAt(pBitList, byteNumber, 1)
      if (showEveryByte  ||  (numByte <> initValue)  ||  (intByte <> initValue)){
         msgbox, 1, Inside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%  baseAddress= %pBitList%)
         IfMsgBox, Cancel
            return
      }
   }
}
GetIntAt(address, offset, size){      ; replacement for NumGet works always fine
   int := 0
   Loop %size% {
      byteNumber := A_index - 1
      int += *(address + offset + byteNumber) << (8 * byteNumber)
   }
   return int
}


Last edited by Peter on November 17th, 2009, 8:14 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: November 17th, 2009, 6:55 am 
Offline

Joined: August 13th, 2006, 6:45 am
Posts: 355
Location: Germany
change the following line without &
Code:
CheckBytes(bitList, GrantedCapacity)

Hubert


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 17th, 2009, 7:19 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Hi Peter.. :)


Code:
CheckBytes(pBitList, GrantedCapacity){
   ; W/ NumGet in a function, it causes always a problem at some byte(s)
   global showEveryByte, initValue
   
   Loop %GrantedCapacity% {
      byteNumber := A_index - 1
      numByte := NumGet(pBitList+0, byteNumber, "char")   ; pBitList or *pBitList does NOT work for me
      intByte := GetIntAt(pBitList, byteNumber, 1)
      if (showEveryByte  ||  (numByte <> initValue)  ||  (intByte <> initValue)){
         msgbox, 1, Inside: byte %byteNumber%  (V%A_AhkVersion%)
                  , numByte= %numByte% `nintByte= %intByte%  `n(initValue= %initValue%  baseAddress= %pBitList%)
         IfMsgBox, Cancel
            return
      }
   }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 17th, 2009, 8:13 am 
Offline

Joined: December 30th, 2005, 5:01 pm
Posts: 448
Thanks Skan. Nice trick. While I saw the trick, I remembered it is mentioned in some way in AHK Help as well.
I guess struggling too long with it, that I overlooked that option.
But actually confusing/weird. Suppose you have to do the same with other functions.
I.e. using Sin(x) outside a function and Sin(x+0) inside a function :? .
I guess the possibility to choose between NumGet(var) and NumGet(&var) is to blame. ( :idea: Better get rid of that choice?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 17th, 2009, 8:26 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Using pVar+0 was side-effect and Laszlo informed it to Mr.Chris, IIRC.

Peter wrote:
I guess the possibility to choose between NumGet(var) and NumGet(&var) is to blame. ( :idea: Better get rid of that choice?)


I am unable to comment as I am already accustomed to these current methods.
There was a huge discussion: Choose naming and syntax for built-in Extract/InsertInteger


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Cerberus, Google [Bot], Google Feedfetcher, Maestr0, rbrtryn, Tipsy3000, XstatyK and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group