dart转成字符串,如何在Dart中将字符串转换为utf8?

I am using aqueduct web api framework to support my flutter app. In my api backend I need to connect local network socket services. My problem is that I can't return the exact string (in tr). So, How can I convert string to utf8 in Dart?

Example:

@httpGet

Future getLogin() async {

Socket.connect('192.168.1.22’, 1024).then((socket) async {

socket.listen((data) {

// Expected return is: 1:_:2:_:175997:_:NİYAZİ TOROS

print(new String.fromCharCodes(data).trim());

xResult = new String.fromCharCodes(data).trim();

print("xResult: $xResult");

}, onDone: () {

print("Done");

socket.destroy();

});

socket.write('Q101:_:49785:_:x\r\n');

});

return new Response.ok(xResult);

}

The return is not in TR-tr language format.

Return text looks like:

1::2::175997:_:NÝYAZÝ TOROS

Correct must be:

1::2::175997:_:NİYAZİ TOROS

UPDATE:

xResult = new String.fromCharCodes(data).trim();

print(xResult);

responseBody = xResult.transform(utf8.decoder);

print(responseBody);

I can print the xResult but cannot print the responseBody after trying convert to UTF8

解决方案import 'dart:convert' show utf8;

var encoded = utf8.encode('Lorem ipsum dolor sit amet, consetetur...');

var decoded = utf8.decode(encoded);

There are also encoder and decoder to be used with streams

File.openRead().transform(utf8.decoder).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值