Solved

Hex

Posted on 1998-12-04
4
584 Views
Last Modified: 2011-10-03
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
Comment
Question by:randyg
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1179213
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1179214
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
 
LVL 1

Expert Comment

by:The_Brain
ID: 1179215
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
 

Author Comment

by:randyg
ID: 1179216
Great answers!!!!!

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now