Click here to Skip to main content
13,502,972 members
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 25 Jan 2005

A CHexEdit Control - For HEX and DEC number input

, 15 Aug 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to write an Edit control used for HEX and DEC number input.

Sample Image - chexedit.JPG


Recently, I was working on a diagnostic software which requires a Edit Control used for HEX/DEC numbers input. The CEdit that comes with MFC is not so good for this job, as it can not prevent the user from typing an invalid HEX number.

Therefore, I created the CHexEdit control derived from CEdit for HEX/DEC numbers input, it is very easy to use.

Features of the CHexEdit control

These are the features of the CHexEdit class:

  • Automatically validate user input and prevent invalid HEX/DEC number format input.
  • Detect HEX/DEC numbers automatically and highlight the difference by coloring the text.
  • Ability to convert HEX to DEC or DEC to HEX numbers by using the menu that comes with it.
  • Redefines Paste, Cut, Delete, Copy operations to avoid invalid input.
  • Easy to manage input values through its GetValue()/SetValue() functions.
  • Easy to implement for MFC developers.

Using the CHexEdit Control

To use CHexEdit, you must go through the following steps:

  1. In your application, add a control variable for you Edit Control.
  2. Change CEdit to CHexEdit in your corresponding .h files, like this:
    CHexEdit m_cEdit;
  3. Optionally, you can define your own HEX/DEC number background/foreground colors when initializing your application.
    //black text color and white text background for hex number
    //white text color and black text background for dec number
  4. Optionally, you can define its default number through SetValue(unsigned int _value,bool _bHex).
    //set value to 100 using HEX format to express
  5. You can get the input value through GetValue().

Technical background

The CHexEdit is derived form CEdit, and there are three modifications, including:

  1. Validate user input. This is done by overriding CEdit::PreTranslateMessage(MSG* pMsg). When a user strikes a key, it determines whether the key is valid, and makes the input letters to upper case making the number look a bit nicer.
  2. Redefine CEdit's default popup menu. Since a user can not all the time do copy, cut, paste, etc., it is good to check the clipboard buffer and make these menu items grayed when the data is not suitable.
  3. Get input number type (HEX/DEC). By intercepting EN_UPDATE message, it checks whether the input number is HEX or DEC, and colors the background and foreground automatically.
    void CHexEdit::OnEnUpdate() {
        //Hex or Dec?
        if(text.Find("0x")== 0)
        for(int i=0; i < text.GetLength(); i++)
            char c=text.GetAt(i);
            if(c >= 'A' && c <= 'F')


  • 21 Jan 2005 - v1.0 - First release.


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


About the Author

Luo Pei'en
Web Developer
China China
No Biography provided

You may also be interested in...


Comments and Discussions

Generalneed your help Pin
billups13-Sep-09 16:21
memberbillups13-Sep-09 16:21 
Generalwhen UNICODE is used, many modify is needed Pin
wps16-May-06 16:27
memberwps16-May-06 16:27 
GeneralNewbie question Pin
egads@Fermi11-May-06 10:39
memberegads@Fermi11-May-06 10:39 
GeneralRe: Newbie question Pin
Luo Pei'en12-May-06 18:26
memberLuo Pei'en12-May-06 18:26 
GeneralRe: Newbie question Pin
egads@Fermi15-May-06 3:47
memberegads@Fermi15-May-06 3:47 
GeneralErrors Pin
DavidCrow31-Jan-06 4:04
memberDavidCrow31-Jan-06 4:04 
GeneralRe: Errors Pin
Luo Pei'en31-Jan-06 4:22
memberLuo Pei'en31-Jan-06 4:22 
GeneralRe: Errors Pin
DavidCrow31-Jan-06 4:57
memberDavidCrow31-Jan-06 4:57 
GeneralRe: Errors Pin
Luo Pei'en31-Jan-06 18:28
memberLuo Pei'en31-Jan-06 18:28 
GeneralGr8 Control and a minor fix Pin
NeoAndersson12321-Sep-05 23:07
memberNeoAndersson12321-Sep-05 23:07 
GeneralJust found a memory leak.... Pin
prcarp9-Aug-05 3:12
memberprcarp9-Aug-05 3:12 
GeneralRe: Just found a memory leak.... Pin
Luo Pei'en14-Aug-05 4:09
memberLuo Pei'en14-Aug-05 4:09 
GeneralStrange characters Pin
dgroves15-Mar-05 6:03
memberdgroves15-Mar-05 6:03 
GeneralThanks! Pin
prcarp26-Jan-05 4:18
memberprcarp26-Jan-05 4:18 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 16 Aug 2005
Article Copyright 2005 by Luo Pei'en
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid