【问题标题】:How to convert Text to Binary in dart (flutter)?如何在飞镖(颤振)中将文本转换为二进制?
【发布时间】:2021-01-06 18:48:19
【问题描述】:

我正在尝试开发一个应用程序,以 dart 语言将文本转换为二进制(反之亦然)。这将是我的第一个颤振应用程序。 我发现 dart:convert 库可以做到这一点,但我仍然不知道如何让它工作。可以使用一些帮助。谢谢!

示例:https://www.rapidtables.com/convert/number/ascii-to-binary.html

【问题讨论】:

标签: flutter dart


【解决方案1】:

https://api.flutter.dev/flutter/dart-core/String/codeUnits.html 请使用它。

https://pub.dev/packages/binary_codec 库也会为您提供帮助 你可以这样使用它:


  /// Encode the generated data
  Uint8List encoded = binaryCodec.encode(exampleObject);

  /// Decode the encoded data
  var decoded = binaryCodec.decode(encoded);

  /// Check if they are the same
  if (exampleObject.toString() == decoded.toString()) {
    /// They should be the same
    print('Success');
  } else {
    /// Else theres a bug in the library
    /// Please send the [exampleObject] that leads here and on which 
    /// platform you are to the author of this library (stefan.zemljic@gmx.ch)
    print('Wait... What???');
  }

【讨论】:

  • String.codeUnits 返回字符串的 UTF-16 表示。如果使用二进制格式,我强烈建议不要使用它,因为这意味着字节序变得相关,这可能需要添加 BOM。对于二进制交换,首选 UTF-8。
猜你喜欢
  • 1970-01-01
  • 2020-05-12
  • 2020-02-13
  • 2019-06-03
  • 2021-10-07
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多