Created
July 18, 2024 17:22
-
-
Save stevenspiel/a35c21378c64aa2c0479564348dac5d4 to your computer and use it in GitHub Desktop.
Convert to hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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