Mark B
Mark B

Reputation: 3140

How do I convert a UTF-8 String into an array of bytes in Dart?

I'm creating a Redis client and would like to create a byte array for sending to the Redis server. To issue commands to the server, I need to convert Dart's UTF-8 strings into a bytes which can be written to a socket.

How can I do this?

Upvotes: 25

Views: 21310

Answers (3)

A.Mushate
A.Mushate

Reputation: 455

for images, they might be base64 encoded refer to this

https://stackoverflow.com/a/65146858/4412553

Image.memory(base64.decode('base64EncodedImageString')),

Upvotes: -1

Delaney
Delaney

Reputation: 1039

For Dart >1.0 this is now done with the convert library.

import 'dart:convert';
List<int> bytes = utf8.encode("Some data");
print(bytes) //[115, 111, 109, 101, 32, 100, 97, 116, 97]

Upvotes: 51

Lars Tackmann
Lars Tackmann

Reputation: 20865

You need to import dart:utf and use its encodeUtf8 function. There is actually a existing redis client for Dart here which makes use of these functions.

Upvotes: 7

Related Questions