Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert ASCII to hex

I want convert ASCII values to hex. In Java, I often use the function:

private static String asciiToHex(String asciiStr) {
    char[] chars = asciiStr.toCharArray();
    StringBuilder hex = new StringBuilder();
    for (char ch : chars) {
        hex.append(Integer.toHexString((int) ch));
    }

    return hex.toString();
}

Is there any method in Dart to convert to hex value like Integer.toHexString in Java?

Example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

youtube.com

Output

796F75747562652E636F6D

>Solution :

The equivalent of Integer.toHexString would be to call .toRadixString(16) on an integer value.

The asciiToHex function can be translated to dart like so:

String asciiToHex(String asciiStr) {
  List<int> chars = asciiStr.codeUnits;
  StringBuffer hex = StringBuffer();
  for (int ch in chars) {
    hex.write(ch.toRadixString(16).padLeft(2, '0'));
  }
  return hex.toString();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading