将string转换为short和char

sinat_25104515 2015-01-06 03:52:53
如题 求代码, 最好言简意赅
...全文
1680 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luciferisnotsatan 2015-01-09
  • 打赏
  • 举报
回复
string 本身是typedef,实际是basic_string<char>。如果你的short是指宽字符,那么要用wstring,这个是basic_string<wchar> 获取c风格字符串,用c_str() 这个成员函数
vcf_reader 2015-01-08
  • 打赏
  • 举报
回复
方法很多。。
「已注销」 2015-01-08
  • 打赏
  • 举报
回复
仅供参考
#include<cstdio>
#include<cstdlib>
using namespace std;

int main(int argv, char **argc)
{
	char buffer[5];
	unsigned char m;
	short int n;

	scanf("%s",buffer);
	n = (short)atoi(buffer);
	m = (unsigned char)n;

	printf("%u\n%c\n",n,m);

	return 0;
}
EganLi 2015-01-08
  • 打赏
  • 举报
回复
不是有string.c_str()吗
zhuxuekui3 2015-01-06
  • 打赏
  • 举报
回复
用 strtol 或者 sscanf或者 stringstream
fly_dragon_fly 2015-01-06
  • 打赏
  • 举报
回复

short s;
char ch;
stringstream ss(str);
ss>>s;
ss>>ch;
赵4老师 2015-01-06
  • 打赏
  • 举报
回复
用sscanf一个函数足矣。
poor_coder 2015-01-06
  • 打赏
  • 举报
回复
这个问题有点奇怪。 string是一个串,只能转换成const char *,string类有现成的成员函数可以实现。 c++11:const char* c_str() const noexcept Get C string equivalent Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object. This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end.
版主大哥 2015-01-06
  • 打赏
  • 举报
回复
atoi

65,199

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧