Click here to Skip to main content
15,893,663 members
Articles / Desktop Programming / MFC
Article

Hex Edit Control

Rate me:
Please Sign up or sign in to vote.
4.14/5 (13 votes)
26 Jun 20011 min read 101.5K   4.3K   30   3
An Hex Editing control that switches between plain text and HEX modes (using RICHED20.DLL)

Sample Image - hexedit.jpg

If you have the need to edit character data in HEX and character modes and there is not enough space on the screen to use a fully featured HEX edit control, then this is for you. This HEX edit control allows the user to edit text in HEX or normal ASCII modes and switch between the two. Current text selection cursor position is preserved when switching between the two modes allowing particular areas of text to be highlighted and viewed in HEX mode.

Non-printable characters are displayed as an upside-down question mark by default although this character can be changed by calling the SetBinaryCharacter method. The public methods available on CHexEditCtrl are:-

public:
    enum DataMode { TEXT_MODE = 0, HEX_MODE };
public:
    void SetBinaryCharacter(TCHAR ch);
	
    virtual void SetDataSelection(const long nFirst, const long nLength);
	
    void DataToScreen();
    void SetData(CString Data, long nLength);
    void GetData(CString& Data, const long nMaxLength, long& nLengthReturned);

    void SetDataMode(const DataMode& mode);

    // only tests data changed since the last call to this function
    BOOL IsDataChanged();	

In order to use the control the steps are

  1. Create a custom control on the screen with a blank caption, class of RichEdit20A, style of 0x50a11084 and exstyle of 0x0.
  2. In the dialogs associated .h file, in the //{{AFX_DATA(... section add the control, eg. CHexEditCtrl m_HexEdit;
  3. In the dialogs associated .cpp file, in the DoDataExchange section add DDX_Control(pDX, IDC_HEXEDIT, m_HexEdit); where the IDC_HEXEDIT matches the resource identifier for the custom control on the dialog

This should be all that is needed to get going. The control may be useful for the display of character messages transferred between hardware devices connected to the PC.

This code works in UNICODE, however, it only displays the hex characters as though it were ASCII.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Hong Kong Hong Kong
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralEnter ASCII in HEX-Mode Pin
ItsMeAgain3-Dec-03 3:39
ItsMeAgain3-Dec-03 3:39 
GeneralRe: Enter ASCII in HEX-Mode Pin
conrad Braam28-Jan-04 0:38
conrad Braam28-Jan-04 0:38 
Generalerror Pin
19-Dec-01 8:40
suss19-Dec-01 8:40 

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.