- I found the way that StrPut works slightly surprising, if you do this:
StrPut(String, Address, Length)
If StrLen(String) > Length, then no text is placed at Address.
- It does actually state this in the documentation:
StrPut / StrGet
https://autohotkey.com/docs/commands/StrPutGet.htm
- So Length acts as a limit, not for truncation, but as a limit for the outputting of any text whatsoever.If Length is less than the length of the converted string, the function fails and returns 0. If Length is exactly the length of the converted string, the string is not null-terminated
- So what you should do is this (to ensure a null character in a variable with a capacity of Length characters):
StrPut(SubStr(String,1,Length-1), Address)
Or this (if no null character is needed):
StrPut(SubStr(String,1,Length), Address)
- This departs slightly from NumPut, which lets you put numbers that are too big for the integer size into the variable, it 'truncates' those numbers, by getting the remainder when dividing by 256**n (where n is the size of the integer in bytes) (doing a bitwise OR with (256**n)-1 would give the same result).
- Here is an example script:
Code: Select all
q:: ;test StrPut: truncate string
vMaxChars := 11 ;including null character
VarSetCapacity(vOutput, vMaxChars << !!A_IsUnicode)
;===============
;put in full or don't put
vText := "abcdefghijklmnopqrstuvwxyz"
StrPut(vText, &vOutput, vMaxChars)
MsgBox, % vOutput ;(blank)
vText := "abcdefghijkl"
StrPut(vText, &vOutput, vMaxChars)
MsgBox, % vOutput ;(blank)
vText := "abcdefghijk"
StrPut(vText, &vOutput, vMaxChars)
MsgBox, % vOutput ;abcdefghijk plus perhaps additional characters
vText := "abcdefghij"
StrPut(vText, &vOutput, vMaxChars)
MsgBox, % vOutput ;abcdefghij
;===============
;put in full or truncate
vText := "abcdefghijklmnopqrstuvwxyz"
StrPut(SubStr(vText,1,vMaxChars-1), &vOutput)
MsgBox, % vOutput ;abcdefghij
;===============
;put in full or truncate (NumPut)
vText := "abcdefghijklmnopqrstuvwxyz"
VarSetCapacity(vData, 1, 0)
NumPut(0x100+123, &vData, 0, "UChar")
MsgBox, % NumGet(&vData, 0, "UChar")
return
Convert string in clipboard from Unicode to specific codepage - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 58#p182858