Recent

Author Topic: Print HEX representation of characters in string  (Read 2056 times)

cris75

  • New Member
  • *
  • Posts: 33
Print HEX representation of characters in string
« on: January 19, 2022, 12:22:31 pm »
I have a TCP client that use lNet and once connected to a server, (onReceive) it stores the server responses in a string that is later added in a StringList;
closing the program the StringList is saved in a log file;
sometime a server can reply with not printable data, for ex. HEX 11 DEVICE CONTROL 1 (DC1);
let's say the server responds with "DC1DC1DC1", I'm stuck trying to get the conversion of each char in the string;
in that case i'd like to obtain something like "DC1DC1DC1" or "11(DC1)11(DC1)11(DC1)", so that the log file it's plain readable for everyone;
how could I achieve that in the simplest way?
Any hint and example is very appreciated, thank you.
Lazarus: 2.2.0
FPC: 3.2.2
32+64bit
Current OS: Win10 64bit

Bart

  • Hero Member
  • *****
  • Posts: 4690
    • Bart en Mariska's Webstek
Re: Print HEX representation of characters in string
« Reply #1 on: January 19, 2022, 01:24:29 pm »
Maybe the function Utf8EscapeControlChars() in unit LazUtf8 can be of help, or modify it to your needs?
If you use emHexC as second paramter the output will be rather distict: e.g. 'foo0x0d' indication 'foo' followed by a CarriageReturn.
emAsciiControlNames will give output like 'foo[NAK]'.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 11543
Re: Print HEX representation of characters in string
« Reply #2 on: January 19, 2022, 01:52:36 pm »
I have implemented ToHexString as a type helper for ordinal types and that is in 3.2.0
Code: Pascal  [Select][+][-]
  1.     Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  2.     Function ToHexString: string; overload; inline;
  3.  
The use is on the simple type, like integer.tohexstring. And for any ordinal.
« Last Edit: January 19, 2022, 02:00:15 pm by Thaddy »
Путин преступник. Россияне дезинформированы.

Bart

  • Hero Member
  • *****
  • Posts: 4690
    • Bart en Mariska's Webstek
Re: Print HEX representation of characters in string
« Reply #3 on: January 19, 2022, 02:12:02 pm »
But that would convert the entire string to Hex, which is hardly readable...

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 11543
Re: Print HEX representation of characters in string
« Reply #4 on: January 19, 2022, 02:15:51 pm »
Bart, then explain to me what he/she wants? Two different interpretations of the requirement.
Путин преступник. Россияне дезинформированы.

Bart

  • Hero Member
  • *****
  • Posts: 4690
    • Bart en Mariska's Webstek
Re: Print HEX representation of characters in string
« Reply #5 on: January 19, 2022, 02:34:17 pm »
I do not know exactly what (s)he wants.
However, the question states that it is about server responses in a log file, and the fact that, when non-printable character are in the response, TS has trouble identifying what exactly was written.

If you convert everything that is written into the logfile into hexadecimal representation of the string, then evaluating server responses by inspecting the log file (just looking at it) is going to be a rather teadious task. Not many of us can translate '48 65 6C 6C 6F 20 57 6F 72 6C 64' back into 'Hello World' just by looking at it (and that is with deliberate spacing added).

OTOH having 'Hello Word[DC1]' in your logfile could easily be understood as 'Hello World' + Device Control 1.
[DC1] is the offical 3-letter ASCII representation of that character (I looked it up when I implemented Utf8EscapeControlChars).

The "problem" with Utf8EscapeControlChars() is that it absolutely not designed with speed in mind (it is meant for use in the Lazarus IDE, which does not do a lot of logging).
If you have a server that uses logging a lot, then having to use Utf8EscapeControlChars() on every log-string may cause slow down.

AlexTP has made suggestions (but he did not provide a patch yet) how to speed up this function (see Issue #39573).

Bart

avra

  • Hero Member
  • *****
  • Posts: 2335
    • Additional info
Re: Print HEX representation of characters in string
« Reply #6 on: January 19, 2022, 03:19:18 pm »
I'm stuck trying to get the conversion of each char in the string;
You create a constant array[0..31] of strings and populate it to use it as a translation table (like 'NUL', 'SOH', 'STX', 'ETX'...). Now all you have to do is to convert each received char into byte, and if value is less then 32 use string from the translation table, otherwise convert value to hex.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

cris75

  • New Member
  • *
  • Posts: 33
Re: Print HEX representation of characters in string
« Reply #7 on: January 19, 2022, 03:29:40 pm »
Guys, thank you all!

@Bart, @Thaddy, sorry to both of you if my question was a little bit confused..   O:-) :D

If you convert everything that is written into the logfile into hexadecimal representation of the string, then evaluating server responses by inspecting the log file (just looking at it) is going to be a rather teadious task. Not many of us can translate '48 65 6C 6C 6F 20 57 6F 72 6C 64' back into 'Hello World' just by looking at it (and that is with deliberate spacing added).
...
Bart

@Bart, you perfectly got the point   :)
Tried it, Utf8EscapeControlChars() is a good solution for me, i find it "enough and effective";
no problem for the speed issue, the exchange between server and client is limited;

I'll give a look to the type helper you suggested too @Thaddy, I never used it until today, thank you for the hint  :)

Thank you too @avra
Lazarus: 2.2.0
FPC: 3.2.2
32+64bit
Current OS: Win10 64bit

Bart

  • Hero Member
  • *****
  • Posts: 4690
    • Bart en Mariska's Webstek
Re: Print HEX representation of characters in string
« Reply #8 on: January 19, 2022, 03:47:25 pm »
I'm stuck trying to get the conversion of each char in the string;
You create a constant array[0..31] of strings and populate it to use it as a translation table (like 'NUL', 'SOH', 'STX', 'ETX'...). Now all you have to do is to convert each received char into byte, and if value is less then 32 use string from the translation table, otherwise convert value to hex.

Which is exactly what the Utf8EscapeControlChars() function does.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4690
    • Bart en Mariska's Webstek
Re: Print HEX representation of characters in string
« Reply #9 on: January 19, 2022, 03:49:51 pm »
AlexTP has made suggestions (but he did not provide a patch yet) how to speed up this function (see Issue #39573).

Implemented in #242f0ac0.
It's now 35-70 times faster (tested with strings containing only chars < #32).

Bart

 

TinyPortal © 2005-2018