Click here to Skip to main content
12,698,775 members (25,322 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

25K views
1.2K downloads
17 bookmarked
Posted

Convert a Binary File to a Hex Encoded Text File

, 5 Dec 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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-05Fixed "Too few arguments" bug when -h passed to program
2014-11-22Initial posting to CodeProject

License

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

Share

About the Author

PJ Arends
President
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 39239506-Dec-14 8:56
memberMember 39239506-Dec-14 8:56 
GeneralRe: My vote of 4 Pin
PJ Arends6-Dec-14 10:29
professionalPJ Arends6-Dec-14 10:29 
GeneralMy vote of 5 Pin
Volynsky Alex27-Nov-14 22:46
professionalVolynsky Alex27-Nov-14 22:46 
GeneralRe: My vote of 5 Pin
PJ Arends28-Nov-14 7:23
professionalPJ Arends28-Nov-14 7:23 
GeneralRe: My vote of 5 Pin
Volynsky Alex28-Nov-14 11:09
professionalVolynsky Alex28-Nov-14 11:09 
GeneralMy vote of 4 Pin
zqck27-Nov-14 4:56
memberzqck27-Nov-14 4:56 
GeneralRe: My vote of 4 Pin
PJ Arends28-Nov-14 7:24
professionalPJ Arends28-Nov-14 7:24 
BugMinor bug Pin
Roger6526-Nov-14 0:47
memberRoger6526-Nov-14 0:47 
GeneralRe: Minor bug Pin
PJ Arends26-Nov-14 5:54
professionalPJ Arends26-Nov-14 5:54 
AnswerRe: Minor bug Pin
Roger6526-Nov-14 6:17
memberRoger6526-Nov-14 6:17 
GeneralRe: Minor bug Pin
PJ Arends5-Dec-14 22:07
professionalPJ Arends5-Dec-14 22:07 
GeneralRe: Minor bug Pin
Roger656-Dec-14 3:27
memberRoger656-Dec-14 3:27 
QuestionNice utility Pin
jeron124-Nov-14 9:23
memberjeron124-Nov-14 9:23 
AnswerRe: Nice utility Pin
PJ Arends24-Nov-14 15:58
professionalPJ Arends24-Nov-14 15:58 
QuestionGood job! Pin
Mihai MOGA23-Nov-14 3:51
professionalMihai MOGA23-Nov-14 3:51 
AnswerRe: Good job! Pin
PJ Arends23-Nov-14 9:11
professionalPJ Arends23-Nov-14 9: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 5 Dec 2014
Article Copyright 2014 by PJ Arends
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid