Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 642
  • Last Modified:

Hex

I am using Visual C++ 5.0 and am in Dialog mode.

I am trying to convert numbers to Hex. I am using this statement and would like to know how to display the number in hex.

SetDlgItemInt(IDC_STATIC,m_number);

I would also like to know how to recieve a hex number from and EDIT box.

Thanks
Randy
0
randyg
Asked:
randyg
  • 2
1 Solution
 
Answers2000Commented:
Say the number is an int

1. Steps are to convert it to a string, e.g.

(a) if want a CString

int nNumber ;
CString str ;
str.Format( "%x", nNumber )

or
(b) if want a char[] array

int nNumber
char str[256] ;
sprintf( str, "%x", nNumber ) ;

or

(c) another way to do (b) is to use itoa, this allows you to specify the base (e.g. you can use this to convert a integer into any reasonable base, e.g.

_itoa( nNumber, str /* char array */, 16 ) ; // 16 = base 16

2. Set the string into the dialog (works with CString or char array)

SetDlgItemText( IDC_STATIC, str ) ;


See my next comment for extracting from edit boxes.


0
 
Answers2000Commented:
Right you can can use SetDlgItemText to place text on a static or edit. (simply replace IDC_STATIC with the id of the control)

To get the current text from an edit use GetDlgItemText, then use sscanf to get convert a hex number to a integer

char szOutput[256] ;
GetDlgItemText( IDC_EDIT, szOutput, sizeof szOutput ) ;
int nNumber ;
sscanf( szOutput, "%x", &nNumber ) ;


incidentally %x means use lower case letters in hex numbers, %X means use upper case

I think that's everything you need ?
0
 
The_BrainCommented:
I agree with Answers2000,  itoa would be a good option, I have written a base x program, which converts any number to any base 2-36.  input a number in anybase and output. If you want it you can have it, just E-mail me.  g_dutoit@hotmail.com.  


0
 
randygAuthor Commented:
Great answers!!!!!

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now