Author Topic: Library modules and example for converting between ascii and compacted hex.  (Read 61 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 6662
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
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

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: