How do I write A in the program do the compiler knows it's a hexadecimal number?
How do I write A in the program do the compiler knows it's a hexadecimal number?
int foo = 0x1234; // this is hex
int bar = 1234; // this is decimal
int baz = 01234; // this is octal
If for some reason you wanted to assign values in binary, instead of representing them in hex, you could use inline assembly:
Nobody I know does it and I've never seen it done, but I guess it's something to knowCode:int foo = 0; _asm mov foo, 01100111b
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
It's not. It introduces a platform dependency (x86) and a compiler dependency (VC++, as _asm is non-standard) for zero gain. You can achieve the same portably with preprocessor or template metaprogramming.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Hexxx,
Something to keep in mind when working in different bases...
Everything in memory is stored in binary. So, if you whither you enter 10 decimal or A-hex, the computer stores it as 1010. (Leading zeros omitted.)
By default, cout will display 10.
This means you can enter a number in decimal, another number in hex and add them together with no problems! You can display the result in octal if you wish!
--------
Hex, decimal, and octal are easy in C++. Other bases (including binary) are a bit trickier.
It doesn't directly relate to your question (there is no hex in the source code), but here's a program that will take input in any base and display it in decimal, hex and octal:
Last edited by DougDbug; 01-19-2006 at 07:35 PM.
that's cool DougDbug, now I can program in trinary
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
so what's the difference between that octal above and entering a decimal with a leading zero? So u mean when I enter any number with a leading zero it's realy in base eight!! :OOriginally Posted by Salem
Exactly. If you think you're smart by aligning all your decimal numbers by padding them with zeros:
You'll get an unpleasant surprise - wrong values for e.g. 024, and compile errors if any such number contains a 8 or 9.Code:int a = 001; int b = 024; int c = 532; int d = 393;
Decimals simply never start with 0.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
edit
Does this hold for other languages like Java and C? Because im sure (at least right now I think!) that I aligned numbers using leading zero's.... or maybe that was just for prepending the '0' charcater to the front (and it would convert to a string) and this would just "look" like a decimal value with a leading zero to the user is this right?Originally Posted by CornedBee
So if a user was to enter a value when prompted from cin with a leading zero, this would really be an octal value in the program? If that's the case I really think someone should have told me this before, and I already went trough over a year of programming courses,
It's the same for Java and C, but it only holds for numeric literals. cin will always read in decimal, unless you use the oct or hex manipulators to set it in a different parsing mode.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
If the OP had posted his compiler & platform...Originally Posted by CornedBee
EDIT: This forum should make you post that info. Maybe if it was part of the User CP and was automatically appended to every thread you start...
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
Where's the fun in that? We wouldn't have so much fun nitpickingIf the OP had posted his compiler & platform...
EDIT: This forum should make you post that info. Maybe if it was part of the User CP and was automatically appended to every thread you start...
other people's code, saying oooh look that's not portable and other stuff
It's the little battles for supremacy which make this board great.
It doesn't matter if it had been applicable to his platform. It's simply an unnecessary portability (and optimization, let's not forget that) problem, no matter what else is in the app.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law