Thread: Writing different bases in C

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Writing different bases in C

    How do I write A in the program do the compiler knows it's a hexadecimal number?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    int foo = 0x1234; // this is hex
    int bar = 1234; // this is decimal
    int baz = 01234; // this is octal

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If for some reason you wanted to assign values in binary, instead of representing them in hex, you could use inline assembly:

    Code:
    int foo = 0;
    _asm mov  foo, 01100111b
    Nobody I know does it and I've never seen it done, but I guess it's something to know
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    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?

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    Quote Originally Posted by Salem
    int foo = 0x1234; // this is hex
    int bar = 1234; // this is decimal
    int baz = 01234; // this is octal
    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!! :O

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Exactly. If you think you're smart by aligning all your decimal numbers by padding them with zeros:
    Code:
    int a = 001;
    int b = 024;
    int c = 532;
    int d = 393;
    You'll get an unpleasant surprise - wrong values for e.g. 024, and compile errors if any such number contains a 8 or 9.

    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

  9. #9
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    edit

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    104
    Quote Originally Posted by CornedBee
    Exactly. If you think you're smart by aligning all your decimal numbers by padding them with zeros:
    Code:
    int a = 001;
     int b = 024;
     int c = 532;
     int d = 393;
    You'll get an unpleasant surprise - wrong values for e.g. 024, and compile errors if any such number contains a 8 or 9.

    Decimals simply never start with 0.
    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?

    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,

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by CornedBee
    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.
    If 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...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    If 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...
    Where's the fun in that? We wouldn't have so much fun nitpicking
    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.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-20-2008, 08:57 AM
  2. Very slow file writing of 'fwrite' function in C
    By scho in forum C Programming
    Replies: 6
    Last Post: 08-03-2006, 02:16 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Difference Between Binary and ASCII writing?
    By Krak in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 12:31 PM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM