Solved

Expressing number in hexadecimal number??

Posted on 1998-07-30
7
405 Views
Last Modified: 2008-03-06
Hi,

I like to ask a question about expressing number in hexadecimal format?( I think, it is hexadecimal).  Why can't we use integer and float number?

What are the good reasons reason behind this?  Can I convert this back to integer number?  For example,

  //----------------------------------------------------//
 static unsigned long SP1[64]  = {

 0x01010400L,0x00000000L,0x00010000L,0x01010404L,
 0x01010004L,0x00010404L,0x00000004L,0x00010000L,
 0x00000400L,0x01010400L,0x01010404L,0x00000400L,
 0x01000404L,0x01010004L,0x01000000L,0x00000004L,
 0x00000404L,0x01000400L,0x01000400L,0x00010400L,
 0x00010400L,0x01010000L,0x01010000L,0x01000404L,
 0x00010004L,0x01000004L,0x01000004L,0x00010004L,
 0x00000000L,0x00000404L,0x00010404L,0x01000000L,
 0x00010000L,0x01010404L,0x00000004L,0x01010000L,
 0x01010400L,0x01000000L,0x01000000L,0x00000400L,
 0x01010004L,0x00010000L,0x00010400L,0x01000004L,
 0x00000400L,0x00000004L,0x01000404L,0x00010404L,
 0x01010404L,0x00010004L,0x01010000L,0x01000404L,
 0x01000004L,0x00000404L,0x00010404L,0x01010400L,
 0x00000404L,0x01000400L,0x01000400L,0x00000000L,
 0x00010004L,0x00010400L,0x00000000L,0x01010004L   };

Thank you very much for answering my question?
 
0
Comment
Question by:ee9553
[X]
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
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1169045
static unsigned long SP1[64]  = {
    16843776L,0L,65536L,16843780L,
    16842756L,66564L,4L,65536L,
    1024L,16843776L,16843780L,1024L,
    16778244L,16842756L,16777216L,4L,
    1028L,16778240L,16778240L,66560L,
    66560L,16842752L,16842752L,16778244L,
    65540L,16777220L,16777220L,65540L,
    0L,1028L,66564L,16777216L,
    65536L,16843780L,4L,16842752L,
    16843776L,16777216L,16777216L,1024L,
    16842756L,65536L,66560L,16777220L,
    1024L,4L,16778244L,66564L,
    16843780L,65540L,16842752L,16778244L,
    16777220L,1028L,66564L,16843776L,
    1028L,16778240L,16778240L,0L,
    65540L,66560L,0L,16842756L  
};

0
 
LVL 7

Accepted Solution

by:
psdavis earned 20 total points
ID: 1169046
Hmm... You question is unclear, but let's see what we can do.

1) The hexadecimal numbers you have displayed are integer as well.  There really isn't a internal conversion, they're the same thing.

If you wanted to display them in hex vs. integer, try the simple printf statements...

printf( "%d %x", SP1[ 0 ], SP1[ 0 ] );

will print out  16843776 and 0x01010400

2) You can convert a hex to a float by using casts as well.

long iNumber = SP1[ 0 ]
float fNumber = (float) SP1[ 0 ];

Tell me if this answers your question.
Phillip
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169047
Integers are always stored as binary numbers.  Regardless of the base you use to enter them (decimal (normal), hexedecimal, or octal), they are converted to a binary number and stored.  C++ doesn't "keep track" of the base in which you entered them.  Thus the two integers

int i1 = 16;
int i2 = 0x10;

are the same.  Integers can be displayed (like using printf()) in a variety of bases.  They do not have to be displayed in the base they were entered in.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1169048
opps, hit submit by mistake.

thus you can enter an integer in one base and print it in another, like

int i = 32;
printf( "%d %x", i,i );

will print the number in both decimal and hex eventhough it was entered in decimal.
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1169049
Nietod,

Don't you wish you could hit tab and actually tab the text instead of going to the submit button?? :)

Phillip
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169050
That wasn't the problem.  I have 4 computers.    I temporarily switched away from the modem computer but I clicked its mouse by mistake rather then the one for the computer I intended to use.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1169051
>> Why can't we use integer and float number?
You can but sometimes it is easier (yes, you read it right) to use hex.

Consider a 32bit integer that holds a bitmask.   That is, each bit acts as a flag (the actual value of the integer is not important).

Suppose I want to turn on some bits and turn off some others.  If C/C++ supported binary format (which it doesn't) I could have written something like:

    flags = [0100101011100001]

However, since binary is not available, one can use hex.  A hex digit corresponds to 4 binary digits.  Thus:

    flags = [0100] [1010] [1110] [0001]

And in hex (this will compile):

    flags = 0x4AE1;

This is for the average human easier to translate back to binary than:

    flags =  19169;

Doesn't it?

Also consider this:

    unsigned long val =  4294967295;

See?  Probably not...  However, once I switch to hex:

    unsigned long val =  0xFFFFFFFF;

It becomes clear that 'val' holds the largest unsigned 32bit value.

All that aside, the real reason is that we C/C++ programmers worry about job security and thus try as hard as we can to make our code incomprehensible to the uninitiated.  Unfortunaly we cwnnot yet program in sanscrit and use base 17 calculations.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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