Reputation: 191
So that you have all the context... in python I run the following code: https://www.delftstack.com/es/howto/python/convert-string-to-ascii-python/
def to_ascii(text):
ascii_values = [ord(character) for character in text]
return ascii_values
text = input("Enter a string: ")
print(to_ascii(text))
If I write "hello" it throws me the following: [104, 101, 108, 108, 111]
My problem is with Flutter. I get that value and since I don't know flutter I don't know how to put it to text (utf8).
builder: (c, snapshot) {
final value = snapshot.data;
...
subtitle: Text(value.toString()),
value.toString() return [104, 101, 108, 108, 111]
How do I put it in utf8 ("Hello")?
Complete code:
@override
Widget build(BuildContext context) {
return StreamBuilder<List<int>>(
stream: characteristic.value,
initialData: characteristic.lastValue,
builder: (c, snapshot) {
final value = snapshot.data;
//convert value ascii to utf8
//var decoded = utf8.decode(value);
return ExpansionTile(
title: ListTile(
title: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Characteristic'),
Text(
'0x${characteristic.uuid.toString().toUpperCase().substring(4, 8)}',
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color: Theme.of(context).textTheme.caption?.color))
],
),
subtitle: Text(value),
contentPadding: EdgeInsets.all(0.0),
),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(
Icons.lightbulb_outline,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5),
),
onPressed: turnOff,
),
IconButton(
icon: Icon(Icons.lightbulb,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: turnOn,
),
//iconButton for add
IconButton(
icon: Icon(
Icons.add_circle,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5),
),
onPressed: extraButton,
),
IconButton(
icon: Icon(
characteristic.isNotifying
? Icons.sync_disabled
: Icons.sync,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: onNotificationPressed,
)
],
),
children: descriptorTiles,
);
},
);
}
}
Upvotes: 2
Views: 10288
Reputation: 4397
In Flutter with built-in dart convert package:
import 'dart:convert';
String convertedValue = utf8.decode(byte);
This will give you the same output as the below Java code:
StringBuilder str = new StringBuilder();
for (byte aByte : bytesArray) {
if (aByte != 0) {
str.append((char) aByte);
} else {
break;
}
Upvotes: 1
Reputation: 13549
The demo:
import 'dart:convert';
void main() {
const asciiDecoder = AsciiDecoder();
final asciiValues = [104, 101, 108, 108, 111];
final result = asciiDecoder.convert(asciiValues);
print(result); //hello
}
see the ref.
Because maxium ascii code is 127, use Base64Decoder instead.
Upvotes: 4
Reputation: 245
import 'dart:convert' show utf8;
var decoded = utf8.decode(value);
See also https://api.dartlang.org/stable/1.24.3/dart-convert/UTF8-constant.html
There are also encoder and decoder to be used with streams
File.openRead().transform(utf8.decoder).
See also
https://www.dartlang.org/articles/libraries/converters-and-codecs#converter
Upvotes: 7