CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Conversion from hex to dec confusion

Hybrid View

  1. #1
    Join Date
    May 2015
    Posts
    245

    Conversion from hex to dec confusion

    Hi,

    In the follwing code (simplified bit)

    Code:
    unsigned short x = 0x18f;
    
    int z = 8;
    
    int y = (x & 0x0f ) + z;
    
    cout << y ;

    it is displaying, 17.. i was thinking it will be 23..

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: Conversion from hex to dec confusion

    23 dec is 17 hex. Have you got a previous cout << hex somewhere? - as hex is sticky and stays until another output type is specified. On my system, the code in post #1 displays 23 in decimal.

    Try :

    Code:
    unsigned short x = 0x18f;
    
    int z = 8;
    
    int y = (x & 0x0f ) + z;
    
    cout << dec << y ;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  3. #3
    Join Date
    May 2015
    Posts
    245

    Re: Conversion from hex to dec confusion

    Thanks a lot kaud, !. yes, i had somewhere else far before the cout << hex !. I also got this, and tried just after the post, with cout << dec. It worked . Not sure why it should stay on !!!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,723

    Re: Conversion from hex to dec confusion

    I tested this code with VS2019.
    I see in the debugger window the value of y being
    y 0x00000017 int
    It is exactly 23 in decimal.
    Victor Nijegorodov

  5. #5
    Join Date
    May 2015
    Posts
    245

    Re: Conversion from hex to dec confusion

    yes i know, was wondering, why it should display in hex, unless i gave << hex explicitly. But it is confusing, that cout retains the previous << hex !!! Thanks Victor

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: Conversion from hex to dec confusion

    AFAIK, the 'sticky' manipulators are:

    Code:
    [no]boolalpha
    [no]showbase
    [no]showpoint
    [no]showpos
    [no]skipws
    [no]unitbuf
    [no]uppercase
    
    dec/ hex/ oct
    
    fixed/ scientific
    
    internal/ left/ right
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  7. #7
    Join Date
    May 2015
    Posts
    245

    Re: Conversion from hex to dec confusion

    Thanks a lot, kaud for the info

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)