Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[How To] Manipulate Binary data with Pointers


  • Please log in to reply
141 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
RE: How to manipulate Binary data with Pointers ? PART 2 ?

Dear Friends, :)

I am working on a kind of tutorial that will explain integers, bitshifting, & structures ( required for DllCall )..

The subject is very vast but I am somewhat familiar with the basics... Just curious to know how many of you would be really interested in getting familiar with the concept ?

It will take time and I will be posting it anyway .. but some feedback from you people will definitely influence the quality of post.

I have already made examples .. but the explanation part is what I am more confused about .. Being more elaborate might make the post boring and making it short would defeat the purpose.

What do you people think ? Would such a post will be useful at all ?

Regards, :)
kWo4Lk1.png

Veovis
  • Members
  • 389 posts
  • Last active: Mar 17 2009 12:24 AM
  • Joined: 13 Feb 2006
I would be very interested! I always love reading your posts because they are advanced (at least for me :D ) topics (like pointers and structures) that are applicable to other languages , but only once you understand them. Since your posts are in the context of AHK, i can learn how to use these kinds of things in the comfort of ahk, and then i know how to use it in harder languages like C++. Keep up the good work!
Posted Image
"Power can be given overnight, but responsibility must be taught. Long years go into its making."

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Michael, :)

Thanks for the feedback.

i can learn how to use these kinds of things in the comfort of ahk, and then i know how to use it in harder languages like C++.


Actually, AHK is the hardest when it comes to handling structures. :(
I have not used it in VB or VC++, but I understand structures are better handled in them.

I had mailed Mr.Chris asking whether InsertInteger() & ExtractInteger() will become built-in ( or whether there would be better implementation in AHK for handling structures ) in near future- for which

It’s planned, but I don’t know when. I’ve been wanting to do it, but other things always seem to jump ahead of it.


So, I am not clear on how useful the post would be, in case Insert/Extract Integer becomes redundant. :roll:

I want to explain in a simple language about : integers ( char,word,dword,qword ), bitshifting, handling structures

Such a post should be helpful to those aspiring to DllCall.

Are there others interested ?

:)
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

RE: <!-- m -->http://www.autohotke... ... 1784#91784<!-- m -->

toHex() does not work as intended ...

Name := "SKAN"
MsgBox, % toHex( Name, 3 ) ; Should be 534B414E
Return


toHex(ByRef b, s = 0) { 
   f = %A_FormatInteger% 
   SetFormat, Integer, H 
   Loop, % VarSetCapacity(b) * !s + s 
      h := h . *(&b + A_Index - 1) 
   SetFormat, Integer, %f% 
   Return RegExReplace(h, "(?:0x)?(.)(?:0x|$)", "0$1") 
}

Please look into it. That function will be referenced in my next post in this topic. I guess, its just a small fix.

Regards, :)
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
(it was fixed a few posts later. See in #94937)

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

it was fixed a few posts later.

The following seems to work just as well:
toHex(ByRef b, l = 0) {
	f := A_FormatInteger, p := &b - 1
	SetFormat, Integer, h
	Loop, % l ? l : VarSetCapacity(b)
		h .= *++p ; unaries rox ur sox!!
	SetFormat, Integer, %f%
	Return, RegExReplace(RegExReplace(h, "0x(.)(?=0x|$)", "0$1"), "0x")
}

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
This is the shortest code I know of (except p := &b, ... h .= *p++ saves another couple of chars), but you can do it slightly faster with setting the capacity of h in advance to 4*(l ? l : VarSetCapacity(B)) and replacing the second RegExReplace with a StringReplace. That will be 3 lines longer, a horrible waste of characters. :lol:

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

you can do it slightly faster with setting the capacity of h in advance to 4*(l ? l : VarSetCapacity(B))

Hadn't thought of this, good idea.

replacing the second RegExReplace with a StringReplace.

You already done that so I wanted to experiment with something different :p

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Laszlo & Titan :)

This is the shortest code I know of


:D I have two versions now. One takes 5 lines and damn slow.
The other one is a 6 liner, but somewhat faster ..

toHexing a 190kb file in my 1.4Ghz Sempron takes:

Laszlo version: 730ms
Titan version: 710ms

My 6 Liner: 1320ms
My 5 Liner: 1930ms

Shortest code need not be the fastest .... I am learning

:( :( :(
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Cool, so StringReplace isn't that much faster then! Like Laszlo said, if you use VarSetCapacity(h, 4 * (l := l ? l : VarSetCapacity(B))) ... Loop, %l% instead it'll be even faster.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

Shortest code need not be the fastest .... I am learning

:( :( :(

:-)
A good VarSetCapacity avoids AHK to reallocate memory several times. This is a costly operation because it has to copy the old content of the memory to the newly allocated one (not always, but I simplify).
For the anecdote, old video games used to repeat some assembly instructions by the number of video lines: it made big code, but avoided the overhead of loop handling (increment, test)...
Not necessarily true with interpreted languages where the overhead of code processing is high.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Cool, so StringReplace isn't that much faster then!


Uh! No!.. I had tried the RegEx version of Laszlo. StringReplace costs only 672ms :shock:

:!:
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

A good VarSetCapacity avoids AHK to reallocate memory several times. This is a costly operation because it has to copy the old content of the memory to the newly allocated one (not always, but I simplify).


I was just reading the help for VarSetCapacity() :D. But it does not help much ... some 30ms savings from a prior value of 1320ms for a 190 KB file. The output variable size is 387838 chars.

Tch!. I discovered an innovative method, only to find it is too slow ... :(
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Try to allocate a little more memory (5*... or 9+4*...) Maybe AHK allocates more room, before the variable is full. Also, in the list of variables you see what was the final length. If it is not exactly twice the output length, something went wrong.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Also, in the list of variables you see what was the final length. If it is not exactly twice the output length, something went wrong.


No Sir .. I think there is nothing wrong in the code .. It is the bad algorithm :( ... That algorithm has to run slow. But it is okay as it is only a demonstration. Personally, I will be using yours .. which is a Cheetah! :D

Thank you. :)
kWo4Lk1.png