Forum OpenACS Q&A: string size

Collapse
Posted by arief zj on
hi... how to measure the size of a string (i mean in byte)? is it
using : bytelength?
Collapse
2: Response to string size (response to 1)
Posted by Connie Hentosh on
Yes bytelength with give you the size of the UTF-8 string. (Since UTF takes 1-3 bytes to represent a character... this can be different than length which will give you the number of characters in the string).

set bytes_of_memory  [string bytelength $temp_str]
set num_of_chars  [ string length $temp_str]

Usually one needs only the number of chars in a string.
Collapse
3: Response to string size (response to 1)
Posted by arief zj on
but both commands give the same answer/result....right????

set a "abcdef"
set b [string bytelength $a]
set c [string length $a]

both gave 6 ...is it correct?

Collapse
4: Response to string size (response to 1)
Posted by Robert Locke on
In the simple case of "abcdef", yes, both give the same result because each character is represented by a single byte.

However, with extended UTF character set encoding, some characters are multi-byte and need to be represented with 2 or 3 bytes.  In those cases, bytelength and length would yield different results.

Collapse
5: Response to string size (response to 1)
Posted by Connie Hentosh on
Try this example:

[dad]$ tclsh
% set yo "Athens u0391u03B8u03AEu03BDu03B1u03B9"
Athens ??????
%  set c [string length $yo]
13
% set c [string bytelength $yo]
19

If you have wish and the correct fonts installed then you can see what the question marks really are by typing:

[dad]$ wish
% set yo "Athens u0391u03B8u03AEu03BDu03B1u03B9"
Athens ??????
% pack [text .t]
% .t insert end $yo

Collapse
6: Response to string size (response to 1)
Posted by Connie Hentosh on
Oh yuck... not the slash problem again :(

There should be a black slash before the u's in the set yo lines.

so they should look like this:

% set yo "Athens u0391u03B8u03AEu03BDu03B1u03B9"

I hope the slashes appear this time.