0

I pass an hexadecimal number into a function which is of type unsigned long and lets say the number is 0x5ED3710573047010. when I display this number it is displayed in decimal form and not hexadecimal. I want to know is that when the value was being assigned to this variable was it converted at the time being assigned to the variable from hex to dec or just the cout is causing it to be displayed in the decimal format? Also if I want to split this hexadecimal number into two parts what is the best way to do it? Should I convert it into a string and split the string then convert the two strings back to an Int and store it in two separate variables? or there is some easy and quick way to do it?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Azeem Lodhi
  • 53
  • 11
  • It is converted to decimal by `cout`. You can use [`std::hex`](https://en.cppreference.com/w/cpp/io/manip/hex) to have it print in hexadecimal. – MikeCAT May 28 '21 at 11:49
  • Does this answer your question? [C++ cout hex values?](https://stackoverflow.com/questions/479373/c-cout-hex-values) – Lukas-T May 28 '21 at 11:50
  • Be careful, `long` could be 32 bits wide, even on a 64 bit system (for example when using the MSVC compiler). If you want a guaranteed 64 bit type (at least) use `long long`. Or a [fixed width type](https://en.cppreference.com/w/cpp/types/integer) like `uint_least64_t` – Some programmer dude May 28 '21 at 11:51
  • @churill somewhat yes but could you please look at the second part of my question which involves the splitting of an hex number into two parts? – Azeem Lodhi May 28 '21 at 12:03
  • 1
    @AzeemLodhi Ask one question per question post. – eerorika May 28 '21 at 12:03
  • To see your the number again in hexadecimal format, do as explained in https://stackoverflow.com/questions/479373/c-cout-hex-values – Audrius Meškauskas May 28 '21 at 12:27
  • @Audrius Meskauskas Actually I want to split a hex number into two that's what I am mostly concerned about – Azeem Lodhi May 28 '21 at 12:29
  • @AzeemLodhi Then please post that as a separate and distinct new question. And please take some time to refresh [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 28 '21 at 12:38

1 Answers1

-1

when I display this number it is displayed in decimal form and not hexadecimal. I want to know is that when the value was being assigned to this variable was it converted at the time being assigned to the variable from hex to dec or just the cout is causing it to be displayed in the decimal format?

All numbers in C++ are represented in radix (aka base) 2 i.e. binary. It's never radix 10 (decimal) nor radix 16 (hexadecimal).

When you input an integer, the text that you write is parsed and depending on which function you use for input, typically the prefix of the integer determines how the text is interpreted. Prefix of 0x signifies hexadecimal, 0 signifies octal and otherwise the text is interpreted as decimal. The result of the process is a non-textual binary representation stored in the memory. This value contains no information about the textual representation, such as the radix, besides the value.

Same parsing described above also applies when the compiler interprets the source code (which is text) that contains integer literals.

When you output an integer as text, the default behaviour is to show the textual representation in decimal. This can usually be controlled, depending on what function you use to do the output.

eerorika
  • 232,697
  • 12
  • 197
  • 326