简体   繁体   English

如何将未签名的字符作为ASCII打印

[英]How can I print unsigned char as ascii

I'm trying to convert unsigned char to ascii text. 我正在尝试将未签名的char转换为ascii文本。 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. 编辑我读了无符号字节流,想知道我怎样才能将ascii文本打印出来。 In printf is only pseudo code, which is wrong. 在printf中只有伪代码,这是错误的。

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

should print the whole string objName and the first character from msgType . 应该打印整个字符串objNamemsgType的第一个字符。

For the first to work you'd have to be sure that objName is really null terminated. 对于第一个起作用的对象,您必须确保objName是null终止的。

Also unsigned char is not the correct type to use for strings use plain char without unsigned . 同样, unsigned char不是用于字符串的正确类型,请使用不带unsignedchar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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