65.9K
CodeProject is changing. Read more.
Home

Hex Edit Control

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.14/5 (12 votes)

Jun 27, 2001

1 min read

viewsIcon

101891

downloadIcon

4320

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.