Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #287 Number of bytes of a string

Assign to n the number of bytes in the string s.

This can be different from the number of characters. If n includes more bytes than the characters per se (trailing zero, length field, etc.) then explain it. One byte is 8 bits.

$n = strlen($s);
import 'dart:convert';
var n = utf8.encode(s).length;
n := len(s)
n := length(s);
use v5.10;
use charnames qw( :full );
use Encode;
my $text = "\N{LATIN SMALL LETTER E WITH ACUTE}";
say length $text;  # prints 1

my $utf8 = Encode::encode('UTF-8', $text);
say length $utf8;  # prints 2
n = len(s.encode('utf8'))
n = s.bytesize
let n = s.len();

New implementation...