convert string into byte in flutter dart
convert string into byte

Hi Guy’s Welcome to ProtoCodersPoint, In this Article will checkout How can we convert String or Number (May be it Integer or double) to a byte array (i.e. A List of bytes) in Dart.

Converting strings & number into byte Array (or also know as byte lists) in flutter dart we can make use of different built-in methods provided within dart language.

Converting a String to a Byte Array

    I found out 3 ways by which we can convert a given String into a byte array by using dart language.

    using utf8.encode() method

    you can use the utf8.encode method from the dart:convert library. This method converts a string into a list of bytes.

    import 'dart:convert';
    
    void main() {
      String input = "Hello, Flutter Developer!";
      List<int> byteArray = utf8.encode(input);
      print(byteArray);
    }
    

    output

    [72, 101, 108, 108, 111, 44, 32, 70, 108, 117, 116, 116, 101, 114, 32, 68, 101, 118, 101, 108, 111, 112, 101, 114, 33]
    

    Using .codeUnits property of the String class

    Yes, the codeUnits property of the String class in Dart returns a list of UTF-16 code units. This is another way to convert a string to a byte array, although the result will be in UTF-16 encoding rather than UTF-8.

    import 'dart:convert';
    
    void main() {
      String text = 'protoCodersPoint.com';
      List<int> bytes = text.codeUnits;
      print(bytes);
    }
    

    Output

    [112, 114, 111, 116, 111, 67, 111, 100, 101, 114, 115, 80, 111, 105, 110, 116, 46, 99, 111, 109]
    

    What is the Differences Between UTF-16 byte Encoding and UTF-8 Encoding

    • UTF-16: Each character is typically represented by two bytes. The codeUnits property directly provides these values.
    • UTF-8: Each character can be represented by one to four bytes, depending on the character. The utf8.encode method handles this conversion.

    Using the runes property of the String class

    In Dart Language the .runes property of the String class provides a way to access the Unicode code points of a string. This is method is very useful while dealing with characters outside the Basic Multilingual Plane (BMP) that cannot be represented by a single UTF-16 code unit.

    void main() {
      String input = "Hello, 🌍!";
      Iterable<int> runeList = input.runes;
      print(runeList); // Output: (72, 101, 108, 108, 111, 44, 32, 127757, 33)
      
      // Convert to a list if needed
      List<int> runeArray = runeList.toList();
      print(runeArray); // Output: [72, 101, 108, 108, 111, 44, 32, 127757, 33]
    }
    

    Output

    (72, 101, 108, 108, 111, 44, 32, 32, 80, 114, 111, 116, 111, 32, ..., 115, 33)
    
    [72, 101, 108, 108, 111, 44, 32, 32, 80, 114, 111, 116, 111, 32, 67, 111, 100, 101, 114, 115, 33]


    Converting a Number into a Byte Array

    In dart, to convert a numbers into a byte array, We have a in-built class i.e. ByteData that comes from dart:typed_data library. This class help you working with low-level data types & convert them into byte array. below is a example:

    converting a integer to Byte Array

    import 'dart:typed_data';
    
    void main() {
      int number = 123456;
      ByteData byteData = ByteData(4);
      byteData.setInt32(0, number);
      List<int> byteArray = byteData.buffer.asUint8List();
      print(byteArray); // Output: [64, 226, 1, 0]
    }
    

    Converting a double to a Byte Array

    import 'dart:typed_data';
    
    void main() {
      double number = 123.456;
      ByteData byteData = ByteData(8);
      byteData.setFloat64(0, number);
      List<int> byteArray = byteData.buffer.asUint8List();
      print(byteArray); // Output: [119, 190, 247, 87, 141, 235, 94, 64]
    }
    

    Conclusion

    String to Byte Array: We can make use of utf8.encode or codeUnits for UTF-16 Encoding.

    Number to Byte Array: We can use ByteData with appropriate methods like setInt32 for integers and setFloat64 for doubles.