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
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

You missed my correction.

No, I did not. By funny result I meant 0x6016026030a00, while the correct output is 6162630a00.

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

I meant 0x6016026030a00, while the correct output is 6162630a00.

Hmm, RegExReplace(RegExReplace(h, "0x(.)(?=$|0x)", "0$1"), "0x") seems to work. I don't know what the impact of two calls would be but it shouldn't be much.

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
Yes, this works. With the AutoHotkey\license.txt the second RegExReplace takes about 20ms, not a noticeable time. So, we have a nice example, when regular expressions speed up the code.

This is only 15ms faster, but two lines longer:
h := RegExReplace(h, "0x(.)(?=0x|$)", "0$1")
   StringReplace h, h, 0x,,All
   Return h


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

Hmm, RegExReplace(RegExReplace(h, "0x(.)(?=$|0x)", "0$1"), "0x") seems to work.


Ah! Then it can be applied to your RgbToHex() !

Thanks!

:)
kWo4Lk1.png

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

This is only 15ms faster, but two lines longer

Cool, looks like I underestimated StringReplace.

Ah! Then it can be applied to your RgbToHex()

Done :)

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
Thanks Titan, I will be using it!

BTW, change the "ownage (req. 1.0.46.x+)" !

:)
kWo4Lk1.png

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

1.0.46.x+

v1.0.46 released: SubStr() and more expression operators

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
Uh Oh! :oops: Thanks! I missed it! .. :D
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
...and this is how your Rotate function looks like in AHK v1.0.46.x+:
Rotate( pointer, dataSz=0, factor=47 ) { ; Defaults to ROT47

   L0 := 32+factor, L1 := 127+factor, pointer--

   Loop %dataSz% {

      Asc := *(++pointer) + factor

      If ( Asc > L0 and Asc < L1 )

         DllCall("RtlFillMemory", Int,pointer, Int,1, UChar,Asc<127 ? Asc : Asc-94)

   }

}


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Sir, Many thanks for giving that nice example! The function looks so beautiful! Will try it shortly.. :D
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
I can see a 50ms savings in AHK's license.txt, but I like it more for being a demonstration for AHK's new capabilities.

I will allow a few days and then update the original post with the function.

Many Thanks & Regards, :D
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
You might want to mention that decryption is done with the key 94-factor. For example
Rotate(&x,StrLen(x),80) ; replace x with encrypted text

Rotate(&x,StrLen(x),14) ; replace x with decrypted original
ROT47 happens to be its own inverse. factor = 13 is NOT ROT13!

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

ROT47 happens to be its own inverse. factor = 13 is NOT ROT13!


Thanks for informing it. I had not tried a number other than 47!

Regards, :)
kWo4Lk1.png

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005

we have a nice example, when regular expressions speed up the code

Not any more. The old style hex converter script is 15% faster with today's AHK release (version 1.0.46.01) than the RegEx version, thanks to the speedup of expressions. The lesson: because AHK is constantly improving, don't trust old speed comparisons.
#NoEnv

SetBatchLines -1



file = C:\Program Files\AutoHotkey\license.txt

FileRead b, %file%

b := b b b b b b b b b b



t2 := A_TickCount

h := tHex(b)

t2 := A_TickCount - t2

t1 := A_TickCount

h := toHex(b)

t1 := A_TickCount - t1



MsgBox Old style = %t2%`nRegEx = %t1%



tHex(ByRef b, s = 0) {

   f := A_FormatInteger

   SetFormat Integer, H

   VarSetCapacity(h, 5*(VarSetCapacity(b) * !s + s))

   Loop % VarSetCapacity(b) * !s + s

      h := h . *(&b+A_Index-1)+256

   StringReplace h, h, 0x1,,All

   SetFormat Integer, %f%

   Return h

}



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%

   h := RegExReplace(h, "0x(.)(?=0x|$)", "0$1")

   StringReplace h, h, 0x,,All

   Return h

}


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

The lesson: because AHK is constantly improving, don't trust old speed comparisons.


Thanks, :)
kWo4Lk1.png