Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I decode a hexadecimal to a byte array in Dart?

Tags:

hex

flutter

dart

So there's the 'dart:convert' library, which contains a HexDecoder class that doesn't seem to have a constructor (according to this). But importing it and trying to construct it doesn't work; I thought maybe there was a default constructor not mentioned.

I could copy the code in the source for the convert method, but I'd rather make this a learning opportunity. Any help would be appreciated.

like image 659
mgabz Avatar asked Apr 17 '19 23:04

mgabz


People also ask

Is Hex same as byte?

Each Hexadecimal character represents 4 bits (0 - 15 decimal) which is called a nibble (a small byte - honest!). A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.

How do you convert strings to hexadecimal in darts?

A powerful conversion of [String] or/and Hash to HEX. It returns a unique HEX, or a unique int of [Color()] per provided String/hash. It's provided two methods [toHexString] and [toColor], which return a Hex-String, or integer of Color() respectively.

What is Uint8List?

Uint8List is a list of integers where the values in the list are only 8 bits each, or one byte. The U of Uint8List means unsigned, so the values range from 0 to 255 . That's perfect for representing binary data!


1 Answers

HexDecoder isn't actually in dart:convert. It's in a package (also) called convert.

You need to add it to your pubspec.yaml and then use an import like:

import 'package:convert/convert.dart';

Then use it like this:

  hex.decode('abcdef');

hex is a const singleton instance of the codec. (The constructor is private; you don't need to instantiate your own - use the existing const instance.)

like image 153
Richard Heap Avatar answered Sep 20 '22 14:09

Richard Heap