Skip to content

Instantly share code, notes, and snippets.

@stevenspiel
Created July 18, 2024 17:22
Show Gist options
  • Save stevenspiel/a35c21378c64aa2c0479564348dac5d4 to your computer and use it in GitHub Desktop.
Save stevenspiel/a35c21378c64aa2c0479564348dac5d4 to your computer and use it in GitHub Desktop.
Convert to hex
import 'dart:convert';
import 'dart:typed_data';
void main() {
String input = "";
Uint8List hexDecoded = hexToBytes(input);
Uint8List base64Decoded = base64Decode(hexDecoded);
String hexEncoded = bytesToHex(base64Decoded);
print(hexEncoded);
}
Uint8List hexToBytes(String hex) {
var result = Uint8List(hex.length ~/ 2);
for (var i = 0; i < hex.length; i += 2) {
var byte = hex.substring(i, i + 2);
result[i ~/ 2] = int.parse(byte, radix: 16);
}
return result;
}
Uint8List base64Decode(Uint8List hexBytes) {
var base64Str = utf8.decode(hexBytes);
return base64.decode(base64Str);
}
String bytesToHex(Uint8List bytes) {
return bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment