Logo

Programming-Idioms

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

Idiom #304 Encode string into UTF-8 bytes

Create the array of bytes data by encoding the string s in UTF-8.

use v5.10;
use open ':std', ':encoding(UTF-8)';
use utf8;
my $text = 'Café';

utf8::encode($text);

my @utf8 = unpack('C*', $text);
use v5.10;
use open ':std', ':encoding(UTF-8)';
use utf8;
use Encode qw(encode);
my $text = 'Café';

my @utf8 = unpack 'C*', Encode::encode 'UTF-8', $text;


using System.Text;
byte[] data = Encoding.UTF8.GetBytes(s);
data := []byte(s)
const data = new TextEncoder().encode(s);
import static java.nio.charset.StandardCharsets.UTF_8;
byte a[] = { 0x43, 0x61, 0x66, (byte) 0xc3, (byte) 0xa9 };
String s = new String(a, UTF_8);
var
  data: pbyte absolute s;
data = s.encode('utf8')
data = s.bytes
let data = s.into_bytes();

New implementation...