cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get there from here? Hex to Ascii from struct field

john doe
Lead
Posted on April 03, 2017 at 05:07

I've been playing with the hal sd card driver today.

printf(' Product name : %lx%x\r\n', pSD_CID_Info.ProdName1, pSD_CID_Info.ProdName2);

where pSD_CID_Info is of the type HAL_SD_CardCIDTypeDef which is

typedef struct

{

  __IO uint8_t  ManufacturerID;  /*!< Manufacturer ID       */

  __IO uint16_t OEM_AppliID;     /*!< OEM/Application ID    */

  __IO uint32_t ProdName1;       /*!< Product Name part1    */

  __IO uint8_t  ProdName2;       /*!< Product Name part2    */

  __IO uint8_t  ProdRev;         /*!< Product Revision      */

  __IO uint32_t ProdSN;          /*!< Product Serial Number */

  __IO uint8_t  Reserved1;       /*!< Reserved1             */

  __IO uint16_t ManufactDate;    /*!< Manufacturing Date    */

  __IO uint8_t  CID_CRC;         /*!< CID CRC               */

  __IO uint8_t  Reserved2;       /*!< Always 1              */

}HAL_SD_CardCIDTypeDef;

the output is

 Product name : 5355303447

but the 0x5355303447 when converted to ascii should read

Product name : SU04G

how do I get there from here?

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 03, 2017 at 06:32

printf(' Product name : %-5s\r\n', &pSD_CID_Info.ProdName1);

{ // perhaps safer

char string[6] = { 0 };

memcpy(string, &pSD_CID_Info.ProdName1, 5);

printf(' Product name : %s\r\n', string);

}

{

char *p = (char *)&pSD_CID_Info.ProdName1;

printf('%c%c%c%c%c\r\n',p[3],p[2],p[1],p[0],p[4]); // Endians as described in your example

printf('%c%c%c%c%c\r\n',p[0],p[1],p[2],p[3],p[4]);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

1 REPLY 1
Posted on April 03, 2017 at 06:32

printf(' Product name : %-5s\r\n', &pSD_CID_Info.ProdName1);

{ // perhaps safer

char string[6] = { 0 };

memcpy(string, &pSD_CID_Info.ProdName1, 5);

printf(' Product name : %s\r\n', string);

}

{

char *p = (char *)&pSD_CID_Info.ProdName1;

printf('%c%c%c%c%c\r\n',p[3],p[2],p[1],p[0],p[4]); // Endians as described in your example

printf('%c%c%c%c%c\r\n',p[0],p[1],p[2],p[3],p[4]);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..