Logo

Programming-Idioms

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

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
using System.Text;
byte[] data = Encoding.UTF8.GetBytes(s);
data := []byte(s)
const data = new TextEncoder().encode(s);
var
  data: pbyte absolute s;
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;


data = s.encode('utf8')
data = s.bytes
let data = s.into_bytes();