How would you go about finding how many bytes some text is?
I have a string variable in Applescript that holds some text, and I need to find out how many bytes that text takes up.
Is this possible with Applescript, or is there a way to do it using ‘call method’?
I already knew about count, but I thought it just returned how many characters there are. I need to know the bytes it takes up, or is it the same thing? If it is the same thing that must mean a character takes up 1 byte.
In AppleScript we have several options. The two main ones are plain ascii, the mac-roman encoding (“string” or “text”) and “Unicode text”. Usually, every character in Unicode text takes 2 bytes (or more), against ASCII, where 1 character fits 1 byte. If your text is “Unicode text”, you may need write the text to a external text file “as Unicode text”, then “get eof” of such file, or you can also use a hack such as this one:
set theText to "asdf" as Unicode text
--> now, we hold Unicode text in var "theText"
--> calculate number of bytes
{chars:count theText, bytes:count (text 65 thru -1 of ({{foo:theText}} as string))}
You can guess the nature of your “text” as follow:
set theText to "asdf" as Unicode text
--> now, we hold Unicode text in var "theText"
--> calculate number of bytes
{chars:count theText, bytes:count (text 65 thru -1 of ({{foo:theText}} as string))}
Can you please explain how this works? Sure enough it does but it’s beyond me.
In this trick you “convert” Unicode text to plain ASCII coercing a record-containing-the-Unicode-text to string. Run simply “{{foo:theText}} as string” and you will see the ASCII representation of such record, where you can extract the data corresponding to the Unicode text (that is the “text 65 thru -1”)