0

I have an integer returning from a function:

   int a = func();

For example, a = 236.

I need to treat this as a hexadecimal representation of an integer, and store its decimal representation in another variable, so that when printed like so:

  printf("%x", variable)

...the output should be:

 236
6
  • 6
    What exactly are you trying to do? int a = 0x236 would seem to achieve the result you want. Commented Jan 15, 2013 at 11:14
  • 4
    You mean int a = 0xec; ? Commented Jan 15, 2013 at 11:14
  • @ring0 No I need it like 0x236. And also I am getting this value to the variable from a function. Commented Jan 15, 2013 at 11:17
  • Your question is incomprehensible. Commented Jan 15, 2013 at 11:19
  • 2
    I rewrote the question and the title, to better match the accepted answer. Commented Jan 15, 2013 at 11:40

2 Answers 2

4

All the values will be stored in binary format only. If you want to print the value in decimal formal print using %d format string or if you want to print the value in hexa decimal then print using %x.

Sameway we can assign the values in decimal and hexadecimal format also.

int a = 236;
int b = 0x236;
printf("%d", a); //it will print 236
printf("%x", a); //it will print EC
printf("%d", b); //it will print 566
printf("%x", b); //it will print 236

We can print the stored binary values in octal formal also using %o format string.

Sign up to request clarification or add additional context in comments.

2 Comments

Values are stored as binary, not hexadecimal.
updated the answer. Actually what I meant was, it will not store ASCII value of 2 3 and 6 in 3 bytes for the value 236
3

If i've understood what you need, this little code snippet will do it

#include <stdio.h>

int main()
{
    char num[128];
    int a=236;
    int hex;

    sprintf(num, "0x%d", a);

    sscanf(num, "%x", &hex);

    printf("Hex: %x\n", hex);
}

output is

Hex: 236

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.