Jump to content

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

Crazy Scripting : StringSlice() for FileToHex


  • Please log in to reply
3 replies to this topic
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
If you ever want to parse a string on 'fixed character width' instead of any delimiters,
StringSlice() would be your solution:

StringSlice( byref str, spos=1, len=90, byref n="" ) {
 Return substr( str, spos, (n:=spos+len)-spos )
}

FileToHex converts binary data to hex, wraps up the result into manageable lines ( each of 90 character width ) and places the text in clipboard.

FileToHex is useful when you need to include and use a binary file in-script.
Some of my scripts that use in-script files are:

[*:3364fskq]Include JPEG/PNG/GIF Image in your script:
How to convert Image data (JPEG/PNG/GIF) ?

[*:3364fskq]Include Bitmap in your script:
Convert image to Ahk Code

[*:3364fskq]Include Font in your script:
Crazy Scripting : Include and Use TrueType font from Script

[*:3364fskq]Include an Icon in your script:
Crazy Scripting : Include an Icon in your script

[*:3364fskq]Include a Dialog Resource in your script: ( Hex is compressed )
ChooseColorA


Code:
[color=#225599]; StringSlice() for FiletoHex - www.autohotkey.com/forum/viewtopic.php?t=8728 [/color]
[color=#225599]; by SKAN Suresh Kumar A N, arian.suresh@gmailcom    |   CD: 10-Oct-2010 / LM: 10-Oct-2010[/color]

File = %1%
IfNotExist, %File%, Return
FileGetSize, dataSz , %File%
If ( dataSz < 1 || dataSz > 32400 ) ; adjust to your needs
 Return

FileRead, Bin, *c %File% ; Thanks HotkeyIt, www.autohotkey.com/forum/viewtopic.php?t=62700
BinToHex( &Bin, dataSz, Hex ), spos := 1

[color=white];[/color][color=darkred]The following is compatibility code for AHK_L and maybe removed if not required[/color]
[color=#808080]If ( A_IsUnicode ) {
 VarSetCapacity( TmpD, Len := (dataSz*4) + 2 )
 DllCall( "MultiByteToWideChar", Int,0,Int,0, Str,Hex,UInt,Len//2, Str,TmpD,UInt,Len )
 Hex := TmpD, VarSetcapacity( TmpD, 0 )
}[/color]

While ( ( Line := StringSlice( Hex, spos, 88, spos ) ) <> "" )
          Text .= A_Space A_Space Line ( Mod(A_Index,180) ? "`n" : "`n`n" )

Clipboard := Text
SoundBeep
Return                                                 ; // end of auto-execute section //

BinToHex(Addr, Len, ByRef Hex) {  ; Machine code function by Laszlo Hars
Static Fun ; C Source/Author Notes: www.autohotkey.com/forum/viewtopic.php?p=135559#135559
 Fun ? False : VarSetCapacity( Fun,68 ), NumPut( 0x000000c3, NumPut( 0x5f0001c6
 , NumPut( 0x5ed7754f, NumPut( 0x41018846, NumPut( 0x410441c2, NumPut( 0x2a05eac0
 , NumPut( 0xd08a0a2c, NumPut( 0x0f24068a, NumPut( 0x01884104, NumPut( 0xc22a05ea
 , NumPut( 0xc0d08a0a, NumPut( 0x2c04e8c0, NumPut( 0x068a1024, NumPut( 0x748b562f
 , NumPut( 0x7eff8510, NumPut( 0x247c8b57, NumPut( 0x04244c8b,Fun )))))))))))))))))
 VarSetCapacity( Hex,64), VarSetCapacity( Hex,0 ), VarSetCapacity( Hex, 2*Len+1,32 )
Return !! DllCall( &Fun, UInt,&Hex, UInt,Addr, UInt,Len, "CDecl" ) 
}

StringSlice( byref str, spos=1, len=90, byref n="" ) {
 Return substr( str, spos, (n:=spos+len)-spos )
}

You may place the script on Desktop and drag-drop files on it or you may put it into your SendTo folder to access the script from Explorer Context Menu.

Installation in XP

Press Win+R to bring the 'Run Dialog'
Type SendTo and click OK to open the 'SendTo' folder
Create a File named 'FileToHex.ahk' and paste my code, save it.

That will be it.

You may now convert file from 'Windows Explorer' by selecting the script from context menu:


Posted Image

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Looks great but I'm having problems getting it to work on vista.
I get all question marks on my clipboard when it is run. This isn't what it's suposed to do is it?
Also, your install instructions don't work on vista but that is easily fixed. I get a message box shown in the quote bellow when I try to run 'SendTo'. With help from google I found that the correct path is: C:\Users\<username here>\AppData\Roaming\Microsoft\Windows\SendTo
The AppData is a hidden folder, so it is only visible when 'Show hidden files and folders' is selected in folder options.

[Window Title]
Location is not available

[Content]
C:\Users\User\SendTo is not accessible.

Access is denied.


[OK]



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

I get all question marks on my clipboard when it is run. This isn't what it's suposed to do is it?


Code has been altered to support AHK_L

Also, your install instructions don't work on vista but that is easily fixed


Thanks! :)

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Amazing! I'll keep this in my SendTo folder.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run