tree: 15a9feecac6a98c8788f184be4b00f43b4591900 [search]
  1. benchmark/
  2. example/
  3. lib/
  4. test/
  5. .gitignore
  6. analysis_options.yaml
  7. AUTHORS
  8. CHANGELOG.md
  9. LICENSE
  10. pubspec.yaml
  11. README.md
pkgs/convert/README.md

Dart CI pub package package publisher

Contains encoders and decoders for converting between different data representations. It's the external counterpart of the dart:convert SDK library, and contains less-central APIs and APIs that need more flexible versioning.

Hexadecimal

The hex codec converts lists of bytes to and from hexadecimal strings, following the Base16 spec.

import 'package:convert/convert.dart';

void main() {
  final bytes = [0, 1, 2, 3, 255];

  // Encode bytes to a hex string.
  final encoded = hex.encode(bytes);
  print(encoded); // 00010203ff

  // Decode a hex string back to bytes.
  final decoded = hex.decode(encoded);
  print(decoded); // [0, 1, 2, 3, 255]
}

Percent (URL) encoding

The percent codec converts lists of bytes to and from percent-encoded (also known as URL-encoded) strings according to RFC 3986.

import 'dart:convert';
import 'package:convert/convert.dart';

void main() {
  final bytes = utf8.encode('hello world!');

  // Encode bytes to a percent-encoded string.
  final encoded = percent.encode(bytes);
  print(encoded); // hello%20world%21

  // Decode a percent-encoded string back to bytes.
  final decoded = percent.decode(encoded);
  print(utf8.decode(decoded)); // hello world!
}

FixedDateTimeFormatter

FixedDateTimeFormatter formats and parses DateTime values using a simple, fixed-length pattern where each format character represents a single digit.

import 'package:convert/convert.dart';

void main() {
  final formatter = FixedDateTimeFormatter('YYYYMMDDhhmmss');

  // Encode a DateTime into a string.
  final encoded = formatter.encode(DateTime.utc(1996, 4, 25, 5, 3, 22));
  print(encoded); // 19960425050322

  // Decode a string into a DateTime (returns UTC by default).
  final dateTime = formatter.decode(encoded);
  print(dateTime); // 1996-04-25 05:03:22.000Z
}