title: Convert Any Image to ASCII Text
body: <p>This has been particularly difficult and annoying to research given the fondness of "ASCII ART".  That is <strong>NOT</strong> what I am trying to do here.  I need to replicate a process from an old software package that basically converts an image, any image, into ASCII text before sending it to a printer.  Here is an example of what it spits out...</p>
  
  <p><a href="https://i.stack.imgur.com/vWhvj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vWhvj.png" alt="ascii image"></a></p>
  
  <p>The first two lines are commands.  The next 315 lines are pure ASCII (well they call it ASCII but its obviously hex, (80 chars ber line), followed by several more commands and setup before executing the print.  </p>
  
  <p>I've written all the software already, with exception to converting the image to ascii.  Originally, and according to the documentation, the printer should accept raw binary data of several image types.  Well, after fighting with that for the past day, I now realize why the manufacturer themselves don't go down that road.  </p>
  
  <p>So, my question is, how best to go about converting an image to ascii (hex) characters to send to the printer?  My first guess would be to iterate through each character and convert it but that also seems like the slowest approach.</p>
  
  <pre><code>Public Shared Sub ConvertImageHex(filepath As String)
      Dim f_convOutBytes() As Byte = File.ReadAllBytes(filepath)
      Dim totalStreamLength As Int32 = f_convOutBytes.Length
      Dim labelOutStream(totalStreamLength) As Byte
      f_convOutBytes.CopyTo(labelOutStream, 0)
      Dim HexValue As String = String.Empty
  
      Dim labelPath As String = IO.Path.GetTempPath &amp; "testasci.lbl"
      If File.Exists(labelPath) Then
          File.Delete(labelPath)
      End If
      Dim fs As FileStream = New FileStream(labelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
      Dim s As StreamWriter = New StreamWriter(fs)
      s.Close()
      fs.Close()
      fs = New FileStream(labelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
      s = New StreamWriter(fs)
  
      Dim i As Int32 = 0
      For Each c In f_convOutBytes
          i += 1
          If i &gt; 38 Then
              s.Write(vbCrLf)
              i = 0
          End If
          HexValue = Convert.ToString(Convert.ToInt32(c), 16)
          s.Write(HexValue)
      Next
      s.Close()
      fs.Close()
      If Not s Is Nothing Then s = Nothing
      If Not fs Is Nothing Then fs = Nothing
  End Sub
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/8tzor.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8tzor.png" alt="enter image description here"></a></p>
  
  <p>Quotes from the docs (Probably too much into the weeds at this point):</p>
  
  <pre><code>d = download data
  type = the type of data that will follow, using standard file name extensions
  ASC - Graphic in ASCII format
  
  ASCII graphics
  ASCII-Graphic format
  The stucture is similar to the IMG format, but uses only ASCII characters, to enable a easy usage for
  host devices or ERP systems.
  Following rules are used:
  • all data are hex bytes, i.e. 0-9 and a-f or A-F
  • The printer waits for data until the defined picture size is received.
  • Spaces and carriage returns can be added on different locations. It is required that a
  carriage return is sent at the end of the picture data.
  • The image data can be compressed with a simple algorithm which is black/white
  optimized.
  • The image data are transmitted from top to bottom, each time from left to right. A value
  byte 80 stands left of 01.
  • The first line describes the width and the height of a picture. Width and height are 16 bit
  values each in the Big-Endian format.
  • Also if the width is not devidable by 8, it is required that the missing pixel must be
  transmitted.
  Each line will be transmitted with following values:
  • Optional repetition factor, caused by 00 00 FF xx, whereby xx describes the amount of
  copies of the current line.
  • Picture data - whereby different descriptions are optional possible:
  a: Zerobytes are displayed through the amount of bytes.Valid input: 00 to FF.
  b: Blackbytes (FF) can also be described through the amount of bytes, beginning
  from 81 (81 means 1 time FF, - valid values are 81 to FF ).
  c: A directly encoded number of bytes starts with 80 - followed by the amount of data, i.e.
  80 03 123456. The amout of transmitted bytes can be between 01 and 7F.
  d: A repeated pattern of arbitrary bytes can be initiated with a sequence 00 nn xx, which
  means that xx bytes will be inserted nn times.
  Example: 00 04 AA generates AAAAAAAA.
  </code></pre>