News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Library modules and example for converting between ascii and compacted hex.

Started by hutch--, October 04, 2019, 08:40:25 AM

Previous topic - Next topic

hutch--

I did this code about 8 years ago for some folks in the US who had to convert binary data to ascii due to a legal requirement. Two functions handle the conversion both ways, ascii to hex and hex to ascii. The reason for the compacted hex was to save space as it still ends up twice the length of the binary data.

This is the example in the attached zip file.

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    $compile exe "chextst.exe"

    #link "chex.lib"
    DECLARE FUNCTION bin2chex(src$) COMMON as STRING
    DECLARE FUNCTION chex2bin(hx$)  COMMON as STRING

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain as LONG

    StdOut "-------------"
    StdOut "Original text"
    StdOut "-------------"
    a$ = _
    "The time has come,' the Walrus said,"+chr$(13,10)+_
    "      To talk of many things:"+chr$(13,10)+_
    "Of shoes and ships and sealing-wax"+chr$(13,10)+_
    "      Of cabbages and kings"+chr$(13,10)+_
    "And why the sea is boiling hot"+chr$(13,10)+_
    "      And whether pigs have wings.'"

    StdOut a$+$CRLF                     ' display original text

    StdOut "-------------"
    StdOut "Compacted HEX"
    StdOut "-------------"
    hx$ = bin2chex(a$)                  ' convert ascii to compacted HEX
    StdOut hx$+$CRLF

    StdOut "------------------"
    StdOut "Reconstructed text"
    StdOut "------------------"
    dest$ = chex2bin(hx$)               ' convert compacted HEX to ascii
    StdOut dest$

    waitkey$

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤