开发者

Convert char * to short and char

开发者 https://www.devze.com 2023-01-06 01:46 出处:网络
char * x=\"a\"; how would i convert it to char y=\'a\'; also if i have a short char * a=\"100\" how can i conv开发者_如何学Goert it to short b=100

char * x="a"; how would i convert it to char y='a';

also if i have a short char * a="100" how can i conv开发者_如何学Goert it to short b=100

thanks


char * x = "a";
char y = *x; //or x[0]


char * a = "100";
short b = atoi(a);

Note that assigning return value of atoi to a short might lead to overflow.

Also read why strtol is preferred over atoi for string to number conversions.


Assuming that's all you wanted to do and didn't care about error checking:

char y= *x;
short b= atoi(a);


  • A char * can be used as an array of chars. To get the first letter, use char y = x[0]
  • A string can be converted to a number using the function atoi
0

精彩评论

暂无评论...
验证码 换一张
取 消