如何将字符串中的单个字符转换为 int
请记住,如果您选择回答这个问题,我是编程领域的初学者,可能需要比其他人更多地解释解决方案的工作原理。
感谢您的帮助。
我的问题是我试图用字符串的一部分(仅由数字组成)进行计算,但我不知道如何将单个字符转换为 int。该字符串被命名为“message”。
for (int place = 0; place < message.size(); place++)
{
if (secondPlace == 0)
{
cout << (message[place]) * 100 << endl;
}
}
谢谢。
Keep in mind, if you choose to answer the question, I am a beginner in the field of programming and may need a bit more explanation than others as to how the solutions work.
Thank you for your help.
My problem is that I am trying to do computations with parts of a string (consisting only of numbers), but I do not know how to convert an individual char to an int. The string is named "message".
for (int place = 0; place < message.size(); place++)
{
if (secondPlace == 0)
{
cout << (message[place]) * 100 << endl;
}
}
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是要将字符
'0'
转换为整数0
,将'1'
转换为1 等,最简单的方法可能如下:
由于数字字符以升序编码为 ascii,因此您可以从中减去
'0'
的 ascii 值相关字符的 ascii 值并获取等于该数字的数字。If you mean that you want to convert the character
'0'
to the integer0
, and'1'
to1
, et cetera, than the simplest way to do this is probably the following:Since the characters for digits are encoded in ascii in ascending numerical order, you can subtract the ascii value of
'0'
from the ascii value of the character in question and get a number equal to the digit.