I missed it because it wasn't as is in a code section.
So, for the record, here is my version:
toHex3(ByRef _data, _dataSize=0) { s := _dataSize = 0 ? VarSetCapacity(_data) : _dataSize a := &_data f := A_FormatInteger SetFormat Integer, H VarSetCapacity(h, 5 * s) Loop %s% h .= *(a++) + 256 StringReplace h, h, 0x1, , All SetFormat Integer, %f% Return h }Note: I wondered what was this 's' parameter in the original script, used in a cryptic way. Titan' style of ultra-short variable names, esoteric use of operators and lack of comments makes hard to understand his scripts. I wouldn't want to have to maintain his code... ;-)
Note that + 256 is faster than + 0x100...
I suppose I could write *a++ but I prefer with parentheses, which auto-document the evaluation order.
I updated my BinaryEncodingDecoding.ahk file with the following code:
/* // Convert raw bytes stored in a variable to a string of hexa digit pairs. // Convert either _byteNb bytes or, if null, the whole content of the variable. // // Return the number of converted bytes, or -1 if error (memory allocation) */ Bin2Hex(ByRef @hexString, ByRef @bin, _byteNb=0) { local dataSize, dataAddress, granted, f ; Get size of data dataSize := _byteNb < 1 ? VarSetCapacity(@bin) : _byteNb dataAddress := [email protected] ; Make enough room (faster) granted := VarSetCapacity(@hexString, dataSize * 5) if (granted < dataSize * 5) { ; Cannot allocate enough memory ErrorLevel = Mem=%granted% Return -1 } f := A_FormatInteger SetFormat Integer, H Loop %dataSize% { @hexString .= *(dataAddress++) + 256 } StringReplace @hexString, @hexString, 0x1, , All SetFormat Integer, %f% Return dataSize }Note that passing the result ByRef is much faster!
Demonstration:
SetBatchLines -1 Process, Priority, , R file := RegExReplace(A_AhkPath, "i)AutoHotkey\.exe$", "license.txt") FileRead b, %file% b := b b b b b b b b b b t1 := A_TickCount Loop 10000 F1(b) t1 := A_TickCount - t1 t2 := A_TickCount Loop 10000 F2(b) t2 := A_TickCount - t2 t3 := A_TickCount Loop 10000 x := F3(b) t3 := A_TickCount - t3 t4 := A_TickCount Loop 10000 F4(x, b) t4 := A_TickCount - t4 MsgBox 1: %t1% / 2: %t2% / 3: %t3% / 4: %t4% F1(b) { a := &b Return a } F2(ByRef @b) { a := [email protected] ; Do a little something, to get non-zero result... Return a } F3(b) { r := b Return r } F4(ByRef @r, ByRef @b) { @r := @b Return }