codeunits dart到编码。Unicode C#

codeunits dart到编码。Unicode C#,c#,dart,C#,Dart,我有一个用C#编写的套接字TCP服务器。客户端请求是Encoding.Unicode.GetString(buffer,0,buffer.Length) 我有一个用dart编写的客户端程序,它通过Socket.add命令发送字符串代码单元 我的问题是,在C#Encoding.Unicode.GetString中,每个字符读取2个字节 我的问题是,in如何从dart读取代码单位到Unicode C#,反之亦然 String str = '3a487186711dc218'; 在Dart中: st

我有一个用C#编写的套接字TCP服务器。客户端请求是Encoding.Unicode.GetString(buffer,0,buffer.Length)

我有一个用dart编写的客户端程序,它通过Socket.add命令发送字符串代码单元

我的问题是,在C#Encoding.Unicode.GetString中,每个字符读取2个字节

我的问题是,in如何从dart读取代码单位到Unicode C#,反之亦然

String str = '3a487186711dc218';
在Dart中:

str.codeUnits = [51, 97, 52, 56, 55, 49, 56, 54, 55, 49, 49, 100, 99, 50, 49, 56];
在C#中:


好的,我想问题解决了

由于dart中的代码单位为Utf16(16位),我可以将每个单位转换为2字节数组:

List<int> convertCodeUnitsToUnicodeByteArray(List<int> codeUnits) {
  ByteBuffer buffer = new Uint8List(codeUnits.length*2).buffer;
  ByteData bdata = new ByteData.view(buffer);
  int pos = 0;
  for(int val in codeUnits) {                
    bdata.setInt16(pos, val, Endian.little);
    pos += 2;
  }
  return bdata.buffer.asUint8List();
}
List ConvertCodeUnits到Unicode字节数组(List codeUnits){
ByteBuffer buffer=新的UINT8列表(codeUnits.length*2).buffer;
ByteData bdata=新的ByteData.view(缓冲区);
int pos=0;
对于(以代码单位表示的int val){
b数据setInt16(位置、值、尾端、小);
pos+=2;
}
返回bdata.buffer.asUint8List();
}

现在从Tcp套接字C#我正确地获得了颤振应用程序发送的字符串。

好的,我想问题已经解决了

由于dart中的代码单位为Utf16(16位),我可以将每个单位转换为2字节数组:

List<int> convertCodeUnitsToUnicodeByteArray(List<int> codeUnits) {
  ByteBuffer buffer = new Uint8List(codeUnits.length*2).buffer;
  ByteData bdata = new ByteData.view(buffer);
  int pos = 0;
  for(int val in codeUnits) {                
    bdata.setInt16(pos, val, Endian.little);
    pos += 2;
  }
  return bdata.buffer.asUint8List();
}
List ConvertCodeUnits到Unicode字节数组(List codeUnits){
ByteBuffer buffer=新的UINT8列表(codeUnits.length*2).buffer;
ByteData bdata=新的ByteData.view(缓冲区);
int pos=0;
对于(以代码单位表示的int val){
b数据setInt16(位置、值、尾端、小);
pos+=2;
}
返回bdata.buffer.asUint8List();
}

现在,从Tcp套接字C#我正确地获得了颤振应用程序发送的字符串。

是Dart UTF-8还是UTF-16LE(作为Encoding.Unicode是)?@John Dart有没有办法将代码单元作为Unicode C#发送?我已经测试了在代码单元之间添加0,但我确信这不是一个好的解决方案…@NicolasG代码单元不是字节,而是UTF-16代码点,因此长度为16位。要获取代码单位,请执行
str.Select(ch=>(int)ch).ToArray()
,它将返回一个int数组,而不是字节数组,反之
var str=new string(codeunits.Select(x=>(char)x).ToArray()
。您能否将Dart字符串转换为UTF-8并发送字节?您可以使用dart:convert中的Utf8Encoder类来获取字节列表。或者您真的需要将文本作为UTF-16发送吗?Dart是UTF-8还是UTF-16LE(作为Encoding.Unicode是)?@John Dart有没有办法将代码单位作为Unicode C发送?我已经测试了在代码单元之间添加0,但我确信这不是一个好的解决方案…@NicolasG代码单元不是字节,而是UTF-16代码点,因此长度为16位。要获取代码单位,请执行
str.Select(ch=>(int)ch).ToArray()
,它将返回一个int数组,而不是字节数组,反之
var str=new string(codeunits.Select(x=>(char)x).ToArray()
。您能否将Dart字符串转换为UTF-8并发送字节?您可以使用dart:convert中的Utf8Encoder类来获取字节列表。或者您真的需要将文本作为UTF-16发送吗?