Click here to Skip to main content
15,892,199 members
Articles / Programming Languages / C++

HexEdit - Window Binary File Editor

Rate me:
Please Sign up or sign in to vote.
4.96/5 (137 votes)
17 Oct 2012MIT45 min read 496K   22.4K   321  
Open-source hex editor with powerful binary templates
// srecord.h - classes to read/write Motorola S-record files
//
// Copyright (c) 2011 by Andrew W. Phillips.
//
// This file is distributed under the MIT license, which basically says
// you can do what you want with it but I take no responsibility for bugs.
// See http://www.opensource.org/licenses/mit-license.php for full details.
//

#ifndef SRECORD_INCLUDED
#define SRECORD_INCLUDED


// Writes Motorola S-records to a file from data in memory
class CWriteSRecord
{
public:
	CWriteSRecord(const char *filename, unsigned long base_addr = 0L, int stype = 3, size_t reclen = 32);
	~CWriteSRecord();
	void Put(const void *data, size_t len, unsigned long address = UINT_MAX);
	CString Error() const { return error_; }

private:
	void put_rec(int stype, unsigned long addr, void *data, size_t len);
	int put_hex(char *pstart, unsigned long val, int bytes);

	CStdioFile file_;
	int stype_;                         // preferred record type: 1 = S1, 2 = S2, 3 = S3
	unsigned long addr_;                // Address of next S record to write
	size_t reclen_;                     // Preferred output record size
	CString error_;                     // Error message for last error (if any)
	int recs_out_;                      // Number of S1/S2/S3 records output so far
};

// This class can be used to read a file containing Motorola S-records into memory.
// It handles any combination of S1,S2,S3 records but the records must be in address order.
class CReadSRecord
{
public:
	CReadSRecord(const char *filename, BOOL allow_discon = FALSE);
	size_t Get(void *data, size_t max, unsigned long &address); // Get a line of data (S1/S2/S3) from the file
	CString Error() const { return error_; }

private:
	int get_rec(void *data, size_t max_len, size_t &len, unsigned long &address);
	unsigned long get_hex(char *pstart, int bytes, int &checksum);
	CStdioFile file_;
	unsigned long addr_;                // Address of next record to be read or -1 if at start
	unsigned long line_no_;             // Line number of last line of text read
	CString error_;                     // Error message for last error (if any)
	int recs_in_;                       // Number of S1/S2/S3 records read so far
	BOOL allow_discon_;                 // Allow discontiguous records
};
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Australia Australia
Andrew has a BSc (1983) from Sydney University in Computer Science and Mathematics. Andrew began programming professionally in C in 1984 and has since used many languages but mainly C, C++, and C#.

Andrew has a particular interest in STL, .Net, and Agile Development. He has written articles on STL for technical journals such as the C/C++ User's Journal.

In 1997 Andrew began using MFC and released the source code for a Windows binary file editor called HexEdit, which was downloaded more than 1 million times. From 2001 there was a shareware version of HexEdit (later called HexEdit Pro). HexEdit has been updated to uses the new MFC (based on BCG) and is once more open source.

Comments and Discussions