Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Hex

Posted on 1998-12-04
4
603 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

839 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