Home > Mobile >  Dart is not printing hex string
Dart is not printing hex string

Time:08-24

I got the string \x01\x01 from a tcp/ip socket, when I try to print it to console, no output is coming

void main() {
  
  var out = "\x01\x01";
  print("printing out as --> $out <--");
  final runes = out.runes.toList();
  print(runes);

}

It gives the output as

printing out as -->  <--
[1, 1]

dart pad link: https://dartpad.dev/?id=854e4479bfec03d7e8fd40621c845567

I tried to use hex package and it gives Non-hex character detected error.

Questions.

  1. How do I print these types of strings to the console?
  2. If some conversion is needed, how do I know data belongs to these type ?

my socket client is like the following

  socket.listen(
    // handle data from the server
    (Uint8List data) async {
      var serverResponse = String.fromCharCodes(data);
      print('Server: $serverResponse');
      final runes = serverResponse.runes.toList();
      print(runes);
    },

EDIT

The input b'\x01\x01' is coming from a python socket server (which I can't modify), and as per this answer which is a valid hex string.

CodePudding user response:

To display a hexa, you have to escape the characters like this:

  var out = '\\x01\\x01';

this will work.

CodePudding user response:

I suspect you have misunderstood what the server is sending.

Given you've not stated the server language I'm going to guess that ` ab = b'\x01\x01' generates a array with two bytes both with the value 1.

If you treat this as an ASCII value then 1 is a non printable character.

As such you need to iterate over the array and convert each byte into a suitable visual format.

This might mean that when you see a 1 that you print x01.

  • Related