Skip to main content
relaxe
Associate III
November 14, 2008
Question

How to convert a (u32 *) to (u8 *)???

  • November 14, 2008
  • 5 replies
  • 2704 views
Posted on November 14, 2008 at 02:29

How to convert a (u32 *) to (u8 *)???

    This topic has been closed for replies.

    5 replies

    relaxe
    relaxeAuthor
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:52

    Ok, this question is rather simple.

    the SDIO+SDCard example's functions give their result in a 32bit pointer, like: u32 *readbuff.

    Now, I want to implement a filesystem library like dosfs or efsl. They all require to have a read/write function wich use a 8 bit pointer, like: u8 *buffer.

    Now, is there an easy way to pass/convert one to another, without have to transfer the u32* buffer to another u8*buffer 8bits at a time?

    I was looking at something like ''u8 *buffer8 = (u8 *) buffer32'' ....

    Is it doable?

    Thanks!

    -Relaxe

    sword_82
    Visitor II
    May 17, 2011
    Posted on May 17, 2011 at 12:52

    Hi Relaxe,

    u8 *buffer8;

     

    u32 *buffer32;

     

    buffer8 = (u8*)buffer32;

    sword_82

    relaxe
    relaxeAuthor
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:52

    Thanks Sword!

    Now, this actually works:

    u32 Buffer32[2] = {0x33323130, 0x37363534};

    u32 *PtrBuffer32 = Buffer32;

    u8 *PtrBuffer8;

    PtrBuffer8 = (u8*) PtrBuffer32;

    printf(''%d\n'', *(PtrBuffer8+0));

    printf(''%d\n'', *(PtrBuffer8+1));

    printf(''%d\n'', *(PtrBuffer8+2));

    printf(''%d\n'', *(PtrBuffer8+3));

    printf(''%d\n'', *(PtrBuffer8+4));

    printf(''%d\n'', *(PtrBuffer8+5));

    And as you can see, I'm not recreating a whole new buffer, just changing the pointer :p

    -Relaxe

    [ This message was edited by: relaxe on 13-11-2008 16:33 ]

    picguy
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:52

    You may find this general purpose cast macro to be useful

    /* Thanks to SM Ryan who provided the

    * LV macro in news:comp.lang.c

    * As given, the macro name was lvaluecast

    * Message-ID:

    */

    #define LV(type,lvalue) (*((type*)((void*)(&(lvalue)))))

    I used it to store an int to a *int like this

    LV(int*,sp) -= extraStackSpace;

    st3
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 12:52

    Quote:

    I'm not recreating a whole new buffer, just changing the pointer

    You probably don't even need to change the pointer - just the cast should do it.

    However, this is not so simple in the other direction; ie, from a u8* to a u32* pointer - because of the alingment requirements of multi-byte objects...

    [ This message was edited by: st7 on 14-11-2008 01:52 ]