StrPut: put in full or truncate Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

StrPut: put in full or truncate  Topic is solved

20 Nov 2017, 23:18

- I wanted to use StrPut to put a string into a variable of a certain size, if the string was too long, I wanted it to be truncated. I believe I've since found the right way to do this, which is explained lower down.
- 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
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 Length acts as a limit, not for truncation, but as a limit for the outputting of any text whatsoever.
- 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
Some comments and code explaining how to use StrGet/StrPut generally.
Convert string in clipboard from Unicode to specific codepage - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 58#p182858
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ntepa and 236 guests