StrPut: put in full or truncate Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Posts: 4293
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
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] [Expand] [Download] GeSHi © Codebox Plus

Some comments and code explaining how to use StrGet/StrPut generally.
Convert string in clipboard from Unicode to specific codepage - AutoHotkey Community

Return to “Ask For Help”

Who is online

Users browsing this forum: leuce, osuki9x, Xeo786 and 40 guests