Values ranging from 0 to 255 in Excel's cells to bytes in a binary file

Discussion in 'Programmer's Corner' started by atferrari, Aug 27, 2013.

  1. atferrari

    Thread Starter AAC Fanatic!

    Jan 6, 2004
    I need to create a binary file with values previously calculated in Excel.

    The amount of data precludes anything manual.

    Can anyone tell what is the straightest way to convert every value in the cell to a byte with possible values ranging from 0 to 255?

    Sorry but I forgot all my VB and I am not C or C# conversant.

    Help is much appreciated.
  2. vpoko

    Active Member

    Jan 5, 2012
    You can do it right from within Excel with VBA using "put". Assuming each cell contains one value between 0 and 255, you'd loop through your cells and write each byte separately to the file. Excel's help has a code sample that shows how to use put.
  3. WBahn


    Mar 31, 2012
    I don't know of any way in Excel to write to a binary file (not counting the .xls file itself, of course) except through VBA. But my guess is that it wouldn't be too hard to find an example for how to do it that wouldn't need much modification.

    Another alternative would be to save export the column you want to a CSV text file and then use a simply Python (or Perl or other simple language) to read in that file and output the binary file.
  4. Austin Clark

    Active Member

    Dec 28, 2011
    Easy :)

    Use the function DEC2HEX()
    this will convert the values 0-255 into their hexadecimal equivalent.
    From that, you can copy the values and paste them into any hex editor, and save to file.

    EDIT: Also, you can use this method to take in other ranges too, say 0-65535, in which case you'd get 2-bytes per cell. So on and so forth.
  5. vpoko

    Active Member

    Jan 5, 2012
    Excellent solution!