Dart UINT8列表中的Int16无法读取,按字节数组强制转换时数据错误

Dart UINT8列表中的Int16无法读取,按字节数组强制转换时数据错误,dart,flutter,Dart,Flutter,我试图从数据中获取int,缩写,数据从websocket中获取,但有些事情是错误的,当我从UInt8List->Byte data->UInt8List中强制转换时,它在数组中添加了2个新的uint8。有人告诉我如何从字节数组中获取int的正确方法吗。(这是big-Endian,我用Swift编写的代码和用Dart编写的基本数据仍然正确)。感谢所有阅读本文的人 我使用的是“dart:typed_data”;并从WebSocket获取数据(dart:io) 还有一些事情正在发生,因为您的代码没有添

我试图从数据中获取int,缩写,数据从websocket中获取,但有些事情是错误的,当我从UInt8List->Byte data->UInt8List中强制转换时,它在数组中添加了2个新的uint8。有人告诉我如何从字节数组中获取int的正确方法吗。(这是big-Endian,我用Swift编写的代码和用Dart编写的基本数据仍然正确)。感谢所有阅读本文的人

我使用的是“dart:typed_data”;并从WebSocket获取数据(dart:io)


还有一些事情正在发生,因为您的代码没有添加任何额外的字节-实际上,它没有使用
数组

此代码:

import 'dart:typed_data';

void main() {
  Uint8List responseData = Uint8List.fromList([0, 1, 0, 1, 0, 1, 49]);
  print(responseData); // UInt8List: [0, 1, 0, 1, 0, 1, 49]
  var byteData = responseData.buffer.asByteData();
  //var array = byteData.buffer.asUint8List();
  //print(array); // UInt8List: [130, 7, 0, 1, 0, 1, 0, 1, 49]
  var shortValue = responseData.buffer.asByteData().getInt16(0);
  print(shortValue); // -32249 ( 2 first byte: [0 ,1] so it must be 1 )
}
印刷品(如预期)

编辑-正如注释中所建议的,您拥有的UINT8列表实际上是一个具有非零偏移量的ByteBuffer上的视图。所以,
responseData.buffer
就是底层缓冲区,它包含额外的字节。最简单的解决方案是复制视图

import 'dart:typed_data';

void main() {
  Uint8List original = Uint8List.fromList([130, 7, 0, 1, 0, 1, 0, 1, 49]);
  print(original);

  Uint8List view = Uint8List.view(original.buffer, 2);
  print(view);
  print(view.buffer.lengthInBytes); // prints 9
  print(view.buffer.asByteData().getUint16(0)); // unexpected result

  Uint8List copy = Uint8List.fromList(view);
  print(copy.buffer.lengthInBytes); // prints 7
  print(copy.buffer.asByteData().getUint16(0)); // expected result
}

还有一些事情正在发生,因为您的代码没有添加任何额外的字节-实际上,它没有使用
数组

此代码:

import 'dart:typed_data';

void main() {
  Uint8List responseData = Uint8List.fromList([0, 1, 0, 1, 0, 1, 49]);
  print(responseData); // UInt8List: [0, 1, 0, 1, 0, 1, 49]
  var byteData = responseData.buffer.asByteData();
  //var array = byteData.buffer.asUint8List();
  //print(array); // UInt8List: [130, 7, 0, 1, 0, 1, 0, 1, 49]
  var shortValue = responseData.buffer.asByteData().getInt16(0);
  print(shortValue); // -32249 ( 2 first byte: [0 ,1] so it must be 1 )
}
印刷品(如预期)

编辑-正如注释中所建议的,您拥有的UINT8列表实际上是一个具有非零偏移量的ByteBuffer上的视图。所以,
responseData.buffer
就是底层缓冲区,它包含额外的字节。最简单的解决方案是复制视图

import 'dart:typed_data';

void main() {
  Uint8List original = Uint8List.fromList([130, 7, 0, 1, 0, 1, 0, 1, 49]);
  print(original);

  Uint8List view = Uint8List.view(original.buffer, 2);
  print(view);
  print(view.buffer.lengthInBytes); // prints 9
  print(view.buffer.asByteData().getUint16(0)); // unexpected result

  Uint8List copy = Uint8List.fromList(view);
  print(copy.buffer.lengthInBytes); // prints 7
  print(copy.buffer.asByteData().getUint16(0)); // expected result
}

当我试图强制转换到另一个类型时,它会抛出异常,因此我认为它的UInt8List:Unhandled exception:type“\u Uint8ArrayView”不是类型的子类型是的,我真的无法理解为什么responseData可以更改,print(responseData);//[0,1,0,1,0,1,49]var shortValue=responseData.asByteData().getInt16(0);打印(短值);//-32249 Uint8List responseTest=Uint8List.fromList([0,1,0,1,0,1,49]);打印(响应测试);//[0,1,0,1,0,1,49]var shortTest=responseTest.buffer.asByteData().getInt16(0);打印(简短测试)//当然,我怀疑您得到的视图的偏移量不为零,例如从
Uint8List.view()
。最简单的解决方法是在做任何其他事情之前先创建一个干净的
Uint8List
<代码>Uint8List copy=Uint8List.fromList(响应数据)然后使用
copy.buffer.asByteData()
。当我试图强制转换到另一个类型时,它会引发异常,因此我认为它的UInt8List:Unhandled exception:type'\u Uint8ArrayView'不是类型的子类型是的,我真的不明白为什么responseData可以更改、打印(responseData);//[0,1,0,1,0,1,49]var shortValue=responseData.asByteData().getInt16(0);打印(短值);//-32249 Uint8List responseTest=Uint8List.fromList([0,1,0,1,0,1,49]);打印(响应测试);//[0,1,0,1,0,1,49]var shortTest=responseTest.buffer.asByteData().getInt16(0);打印(简短测试)//当然,我怀疑您得到的视图的偏移量不为零,例如从
Uint8List.view()
。最简单的解决方法是在做任何其他事情之前先创建一个干净的
Uint8List
<代码>Uint8List copy=Uint8List.fromList(响应数据)然后使用
copy.buffer.asByteData()