The new kbmMW Features blog post serie will talk about various smaller, but useful, features within kbmMW. This blog will be about the new upcoming TkbmMWPrettyBinary class found in kbmMWGlobal.pas.

kbmMW’s logging framework has for quite a while had the ability to convert binary data to “pretty” readable string data. Since such a feature can have broader use cases, I decided to extract it  from the logging framework into a selfcontained class of its own.

It is very simple to use:

var
   bp:TkbmMWPrettyBinary;
   s:string;
   ba:TkbmMWBytes;
begin
     // Produce some demo data.
     SetLength(ba,10);
     ba[0]:=1;
     ba[2]:=48;
     ba[3]:=49;
     ba[4]:=50;
     ba[5]:=2;
     ba[6]:=3;
     ba[7]:=4;
     ba[8]:=13;
     ba[9]:=10;

     bp:=TkbmMWPrettyBinary.Create;
     try
        s:=bp.Bytes2PrettyString(ba);
        // s now contains: '<SOH> <NUL> 0 1 2 <STX> <ETX> <EOT> <CR> <LF>'

        ba2:=bp.PrettyChar2Bytes(s);
        // ba2 now contains (1, 0, 48, 49, 50, 2, 3, 4, 13, 10)

        s:=bp.Bytes2HexString(ba,8);
        // s now contains: '00000000 :  01 00 30 31 32 02 03 04     <SOH><NUL>012<STX><ETX><EOT>' 
        //                 '00000008 :  0D 0A                       <CR><LF>'

     finally
        bp.Free;
     end;
end;

By setting the property bp.PrettyChar[AIndex:TkbmMWBPPrettyChar]:string its possible to change the conversion of supported binary characters.

 

There are various formatting options available for each of the methods, and overloaded versions operating on TkbmMWStringBuilder is also available.

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.