简体   繁体   中英

How can I print unsigned char as ascii

I'm trying to convert unsigned char to ascii text. How can I do it? I have tried this, but it is wrong:

typedef struct mtmheader
{
  unsigned char objName[20]; 
  unsigned char msgType[2];  
}mtmheader;

mtmheader h;
processMTMHeader(datap,&h, endmmsgp );
printf("Name, type: %s %c\n",(char *) &h.objName,(char *) &h.msgType);

EDIT I read stream of unsigned bytes and want to know what how can i print is as ascii text. In printf is only pseudo code, which is wrong.

printf("Name, type: %s %c\n", h.objName, h.msgType[0]);

should print the whole string objName and the first character from msgType .

For the first to work you'd have to be sure that objName is really null terminated.

Also unsigned char is not the correct type to use for strings use plain char without unsigned .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM