416-621-9348 cgreaves@chrisgreaves.com Visit www.ChrisGreaves.com for this image! Chris_GEDC1894_Head (Small).JPG
Home Services Products

Embedded images (Home) ; Obtain set of original images on disk ; Build application ; Try with images embedded as images in a GUI form within the application ; Switch to images embedded as numeric strings within the application ; Converting a stored image to an ASCII string ; Converting an ASCII string to a stored image ; Optimizing storage of the embedded string ; Function to insert an image from an embedded string ; Macro to drive the function ; Essay with a string as a parameter (statement) ; Converting An Embedded String To A String ; Converting A String To An Array Of VBA Statements ; Converting an string of VBA assignment statements to an array of VBA code ; Converting an array of VBA code to a macro .


Loading

Toronto and Mississauga, Sunday, December 05, 2010 3:06 PM

Copyright © 1996-2010 Chris Greaves. All Rights Reserved.

Converting A Stored Image To An ASCII String

Jefferson Scher set me off in this direction, and I’m grateful, because it turned out to be The Right Way!

Remember that a stored image is just a collection of 8-bit bytes, and if we can read the file as binary data, we can treat each byte of the file as an 8-bit byte.

The main problem is that we can’t stire bytes as characters, because, well, some of them will appear as control characters and wreak havoc with our code.

Jefferson suggested converting each 8-bit byte to its ASCII code, and then storing the ASCII code a s astring.

For example, we know the Tab character as ASCII 009, so if we we convert the byte to its ASCII form, as a long integer, and then format that long interegr to a string.

The single byte represented (in VBA) by Chr(009) thus becomes a 3-character string “009”. If we include a delimiter, then each byte will be represented by a string of two to four characters (CHR(000) yields “ 0” while CHR(255) yields “ 255”).

You will notice that I prefaced each string with a space charactter.

I am a firm believer in self-descriptive delimited strings, where the delimiter character appears as the first or left-most character. Any program code that has to parse the string merely peels the leading character off as the delimiter and goes to work.

Thus “ 9 13 10 58 123” and “,9,13,10,58,123” and “;9;13;10;58;123” are all example sof the same string in self-descriptive form, each with a different delimiter; but the parsing code will remain unchanged.

With this technique in hand, the developer appliaction can read the text file, dteermine the image file, obtain the image file in binary format, and convert that file to a self-descriptive delimited character string.

Be warned. A 20 kb JPG file becomes a 40 kb character string quite easily.


Loading

Toronto and Mississauga, Sunday, December 05, 2010 3:06 PM

Copyright © 1996-2010 Chris Greaves. All Rights Reserved.