This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/MSP430FR5969: How to convert a uint64_t to uint8_t for UART?

Part Number: MSP430FR5969


Tool/software: Code Composer Studio

Hello, 

I have a uint64_t (64 bit) number that I need to send over via 8 bit UART, how would i do that?

So for example:

uint64_t x = 4626624930238680606;

and I have a code (below) that stores the user input before sending over to Raspberry Pi via UART, but instead of storing user input I want to sent "x" instead.

uint8_t Message[] = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd', '1', '2', '3', '4', '5', '6'};
uint8_t Data[16];

// Write a line of text to the terminal
//myUart_writeBuf( CHANNEL_1, (unsigned char *)val, NULL, 0 );
__delay_cycles(10000000);
__enable_interrupt();

unsigned int size = sizeof(Message);
unsigned int sizeMod16 = size%16;
if(sizeMod16!=0)
{
    for (i=0;i<sizeMod16;i++)
    {
        Data[i]=Message[i];
    }
    for (i=0;i<16-sizeMod16;i++)
    {
        Data[sizeMod16+i]=0x00;
    }
}
else if(size == 16){
    for (i=0;i<16;i++)
    {
        Data[i]=Message[i];
    }
}

How do I do that?

-Shawn

  • Typo error:
    "...but instead of storing user input I want to store "x" instead."
  • Hello,
    I have moved this thread to the compiler forum where the compiler experts can guide you best.

    Thanks
    ki
  • I don't know about the compiler experts, this is a C problem.

    use a union (This is not strictly legal C, but it works with most compilers):

    union x {
    uint64_t big;
    uint8_t[8] chunks;
    } pick_a_part;

    ...

    pick_a_part.big = data_to_be_split;

    uart_send(pick_a_part.chunks[0] ); and so on.

    I am not sure whether this syntax is absolutely correct. And I am not sure whether chunks[0] is the MSB or the LSB.
  • Keith Barkley said:
    I am not sure whether chunks[0] is the MSB or the LSB.

    LSB.  

    And I agree that this is a reasonable solution.

    Thanks and regards,

    -George

  • Even though this is not strictly portable (Harbison and Steele say that you can only access a type after you have assigned to it) Are there any compilers that don't allow type punning?
  • Keith Barkley said:
    Even though this is not strictly portable (Harbison and Steele say that you can only access a type after you have assigned to it) Are there any compilers that don't allow type punning?

    Whether or not it's portable is debatable. However, in this situation we can avoid the whole discussion. It's perfectly well-defined to access the bytes of any object through any character type. Assuming an 8-bit char, you can just do

    size_t i;
    uint64_t x = 4626624930238680606;
    unsigned char *bytes = (unsigned char*)&value;
    for(i=0; i<sizeof(x); ++i) uart_write(bytes[i]);
    

    Markus

  • I'll add one detail.  The union method of type punning described by Keith Barkley in a previous post is specifically approved according to the C99 standard for C.  In section 6.5 paragraph 7 it states ...

    An object shall have its stored value accessed only by an lvalue expression that has one of the following types: ... - an aggregate or union type that includes one of the aforementioned types among its members.

    Thanks and regards,

    -George

  • Good, I guess I need new references!