Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C++
Tip/Trick

Convert a Binary File to a Hex Encoded Text File

Rate me:
Please Sign up or sign in to vote.
4.78/5 (9 votes)
5 Dec 2014CPOL 60.8K   2.1K   18   16
A small command line utility to convert a binary file to hex encoded text file

Introduction

When writing some shared code that depends on resources, I find it is often easier to include the resource in the source code rather then supplying a separate resource file that needs to be kept track of and added separately to a project. The thing is the resource needs to be in the source in Hex format so it can be accessed as a byte array. But the conversion can be a bit of a pain unless you have a little utility to do the work for you. Presented here is a command line utility that does just that.

Using the Code

Bin2Text Help Screen.
Usage: Bin2Text [-c Count] [-h] [-s Skip] Input Output

Options:
        -c        Number of bytes to convert to text
         Count  = Number of bytes to convert, '-1' goes to end of file
        -h        Display Usage Information (This screen)
        -s        Skip the first number of bytes in the input file
         Skip   = Number of bytes to skip

Arguments:
        Input   = File to read
        Output  = File to write

  (c) 2015 PJ Arends

The input file can be any file at all. The output file will be a text file where the data is written out in comma separated hexadecimal format, with 16 bytes per line. For example, I have a 5 x 5 pixel 24bit colour bitmap called sample.bmp.

Bin2Text Sample.bmp Sample.txt

And the resulting file Sample.txt:

0x42, 0x4D, 0x86, 0x00,   0x00, 0x00, 0x00, 0x00,   0x00, 0x00, 0x36, 0x00,   0x00, 0x00, 0x28, 0x00, 
0x00, 0x00, 0x05, 0x00,   0x00, 0x00, 0x05, 0x00,   0x00, 0x00, 0x01, 0x00,   0x18, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x50, 0x00,   0x00, 0x00, 0x00, 0x00,   0x00, 0x00, 0x00, 0x00,   0x00, 0x00, 0x00, 0x00, 
0x00, 0x00, 0x00, 0x00,   0x00, 0x00, 0x57, 0x7A,   0xB9, 0x57, 0x7A, 0xB9,   0xC9, 0xAE, 0xFF, 0xC9, 
0xAE, 0xFF, 0x0E, 0xC9,   0xFF, 0x00, 0x15, 0x00,   0x88, 0x57, 0x7A, 0xB9,   0x57, 0x7A, 0xB9, 0xC9, 
0xAE, 0xFF, 0xC9, 0xAE,   0xFF, 0x00, 0x15, 0x00,   0x88, 0x15, 0x00, 0x88,   0x57, 0x7A, 0xB9, 0x57, 
0x7A, 0xB9, 0xC9, 0xAE,   0xFF, 0x00, 0x24, 0x1C,   0xED, 0x15, 0x00, 0x88,   0x15, 0x00, 0x88, 0x57, 
0x7A, 0xB9, 0x57, 0x7A,   0xB9, 0x00, 0x24, 0x1C,   0xED, 0x24, 0x1C, 0xED,   0x15, 0x00, 0x88, 0x15, 
0x00, 0x88, 0x57, 0x7A,   0xB9, 0x00, 

Now the output file can be copy and pasted into my source code.

Updates

2014-12-05 Fixed "Too few arguments" bug when -h passed to program
2014-11-22 Initial posting to CodeProject

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
President
Canada Canada
Father of two, brother of two, child of two.
Spouse to one, uncle to many, friend to lots.
Farmer, carpenter, mechanic, electrician, but definitely not a plumber.
Likes walks with the wife, board games, card games, travel, and camping in the summer.
High school graduate, college drop-out.
Hobby programmer who knows C++ with MFC and the STL.
Has dabbled with BASIC, Pascal, Fortran, COBOL, C#, SQL, ASM, and HTML.
Realized long ago that programming is fun when there is nobody pressuring you with schedules and timelines.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Pedro Buendia6-Dec-14 7:56
Pedro Buendia6-Dec-14 7:56 
GeneralRe: My vote of 4 Pin
PJ Arends6-Dec-14 9:29
professionalPJ Arends6-Dec-14 9:29 
GeneralMy vote of 5 Pin
Volynsky Alex27-Nov-14 21:46
professionalVolynsky Alex27-Nov-14 21:46 
GeneralRe: My vote of 5 Pin
PJ Arends28-Nov-14 6:23
professionalPJ Arends28-Nov-14 6:23 
GeneralRe: My vote of 5 Pin
Volynsky Alex28-Nov-14 10:09
professionalVolynsky Alex28-Nov-14 10:09 
GeneralMy vote of 4 Pin
zqck27-Nov-14 3:56
zqck27-Nov-14 3:56 
GeneralRe: My vote of 4 Pin
PJ Arends28-Nov-14 6:24
professionalPJ Arends28-Nov-14 6:24 
BugMinor bug Pin
Roger6525-Nov-14 23:47
Roger6525-Nov-14 23:47 
GeneralRe: Minor bug Pin
PJ Arends26-Nov-14 4:54
professionalPJ Arends26-Nov-14 4:54 
AnswerRe: Minor bug Pin
Roger6526-Nov-14 5:17
Roger6526-Nov-14 5:17 
GeneralRe: Minor bug Pin
PJ Arends5-Dec-14 21:07
professionalPJ Arends5-Dec-14 21:07 
GeneralRe: Minor bug Pin
Roger656-Dec-14 2:27
Roger656-Dec-14 2:27 
QuestionNice utility Pin
jeron124-Nov-14 8:23
jeron124-Nov-14 8:23 
AnswerRe: Nice utility Pin
PJ Arends24-Nov-14 14:58
professionalPJ Arends24-Nov-14 14:58 
QuestionGood job! Pin
Ștefan-Mihai MOGA23-Nov-14 2:51
professionalȘtefan-Mihai MOGA23-Nov-14 2:51 
AnswerRe: Good job! Pin
PJ Arends23-Nov-14 8:11
professionalPJ Arends23-Nov-14 8:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.