Assign octal/hex declared INT/UINT to another variable

1.1k views Asked by At

My WIN32 (C++) code has a UINT lets call it number. The value of this UINT (or INT doesn't matter) start with a 0 and is recognized as an octal value. It's possible to use the standart operators and the value will keep the octal-system. The same is possible with hex (with foregoing 0x). The problem is I have to use the Value of number in a buffer to calculate with it without changing the value of number. I can assign a value like 07777 to buffer on declaration line but if use an operation like buffer = number the value in buffer is recognized on decimal base.

Anybody has a solution for me?

1

There are 1 answers

0
Keith Thompson On

There's no such thing in C as an "octal value". Integers are stored in binary.

For example, these three constants:

  • 10
  • 012
  • 0xA

all have exactly the same type and value. They're just different notations -- and the difference exists only in your source code, not at run time. Assigning an octal constant to a variable doesn't make the variable octal.

For example, this:

int n = 012;

stores the value ten in n. You can print that value in any of several formats:

printf("%d\n", n);
printf("0%o\n", n);
printf("0x%x\n", n);

In all three cases, the stored value is converted to a human-readable sequence of characters, in decimal, octal, or hexadecimal.

Anybody has a solution for me?

No, because there is no actual problem.

(Credit goes to juanchopanza for mentioning this in a comment.)