0

I am trying to load 32-bit hexadecimal into char array.

#define NUC 0xA8051701

unsigned char debug_msg[100];
sprintf (debug_msg, "%08x", NUC);

But it is loading only "A805" that to as ASCII character instead of hexadecimal. Can any one suggest what could be the problem.

What I'm actually looking for is:

debug_msg[0]=0xA8
debug_msg[1]=0x05
debug_msg[2]=0x17
debug_msg[3]=0x01

Instead of the correct value, it is loading as below:

debug_msg[0]=0x30
debug_msg[1]=0x30
debug_msg[2]=0x30
debug_msg[3]=0x30
debug_msg[4]=0x61
debug_msg[5]=0x38
debug_msg[6]=0x30
debug_msg[7]=0x35

Effectively it is loading 0x0000a805 that to in ASCII.

30
  • sprintf takes a char *, e.g. char debug_msg[100]; Commented Mar 24, 2017 at 5:38
  • 3
    What is the size of int on your machine? Is it a big-endian architecture? What do you actually want as the result? (I guess that sizeof(int) == 2 — 16 bits, and it is big-endian, and you want debug_msg[0] = 0xA8; debug_msg[1] = 0x05; debug_msg[2] = 0x17; debug_msg[3] = 0x01; — but that's guesswork. You should certainly explain what you want and it might help to identify the processor type and so on.) Commented Mar 24, 2017 at 5:39
  • 1
    "But it is loading only "A805" that to as ascii character instead of hexadecimal." . How did u confirmed that? Commented Mar 24, 2017 at 5:55
  • 1
    @Vinodkumar Please let me know the output of printf("%s",debug_msg); Commented Mar 24, 2017 at 6:02
  • 1
    Replace NUC with a variable holding the value to convert. The whole thing macroizes easily. But you should have explained all this in the question without us having to dig it out tidbit by tidbit. Commented Mar 24, 2017 at 6:42

2 Answers 2

3

On the face of it, you need:

 debug_msg[0] = (NUC >> 24) & 0xFF;
 debug_msg[1] = (NUC >> 16) & 0xFF;
 debug_msg[2] = (NUC >>  8) & 0xFF;
 debug_msg[3] = (NUC >>  0) & 0xFF;

(where the >> 0 is optional but makes it look neater — if the compiler optimizes to omit any shift for that). If you want to handle different values in place of NUC, then:

unsigned long value = 0xA8051701;

debug_msg[0] = (value >> 24) & 0xFF;
debug_msg[1] = (value >> 16) & 0xFF;
debug_msg[2] = (value >>  8) & 0xFF;
debug_msg[3] = (value >>  0) & 0xFF;

Or in macro form:

#define MANGLE(value, debug_msg) \
    debug_msg[0] = (value >> 24) & 0xFF; \
    debug_msg[1] = (value >> 16) & 0xFF; \
    debug_msg[2] = (value >>  8) & 0xFF; \
    debug_msg[3] = (value >>  0) & 0xFF

used as:

MANGLE(0xA8051701, debug_msg)

or, if you want the values at arbitrary offsets in the array:

#define MANGLE(value, debug_msg, offset) \
    debug_msg[offset+0] = (value >> 24) & 0xFF; \
    debug_msg[offset+1] = (value >> 16) & 0xFF; \
    debug_msg[offset+2] = (value >>  8) & 0xFF; \
    debug_msg[offset+3] = (value >>  0) & 0xFF

Used as:

MANGLE(0xA8051701, debug_msg, 24);

There might be a need to wrap the body of the macro in a do { … } while (0) loop to make it work properly after an if statement, etc.

Or you could write an inline function to do the job. Or …

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

2 Comments

excellent its working. But if i increase to 64-bit i could not get correct values ie. from 24 to 32 to 40 to 48
You're on an 8-bit micro. You'll need to read how the compiler handles 64-bit numbers, if it handles them at all. If it supports them, you can extend the macro to do more shifting, but you have to be careful about types and so on.
0

You can use a Union here

#define NUMBER_OF_BYTES_FOR_UINT    4
Union Char_UInt
{
  U8 u8data[NUMBER_OF_BYTES_FOR_UINT];
  U32 u32data;
};

Now, you can assign "NUC" (or any 32-bit number) to "u32data" and then read "u8data", It will give you byte-by-byte values.

This is because union assigns same memory to character array and u32data. You write and read from same memory location but through different interfaces.

Note that, Endianness of system plays a role here.

2 Comments

The platform i'm workiing is 8051 microcontroller with keil as IDE.
@Vinodkumar So you can use it as it is

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.