我正在尝试将字符串中的每个字母转换为ASCII数字。使用
int letter = (atoi(ptext[i]));
给我这个错误:
error: incompatible integer to pointer conversion
passing 'char' to parameter of type 'const char *'; take the
address with & [-Werror]
int letter = (atoi(ptext[i]));
^~~~~~~~
&
/usr/include/stdlib.h:148:32: note: passing argument to parameter
'__nptr' here
extern int atoi (__const char *__nptr)
这是我其余部分可能相关的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, string argv[])
{
printf("Give a string to cipher:\n");
string ptext = GetString();
int i = 0;
if (isupper(ptext[i]))
{
int letter = (atoi(ptext[i]));
}
}
我在做什么错,如何解决此问题,以便可以将字符串转换为ASCII值?
注意:cs50.h
我可以在主目录中使用“ string
”代替“ char*
”。
您不需要将字符转换为数字。这是数据解释的问题。
字符'A'可以认为是0x41或65,所以这很好:
int number = 'A';
变量号的值为0x41 / 65或1000001b,具体取决于您要如何显示/处理它。
至于解释:如果将0xFF表示为无符号值,则可将其视为255;当视为有符号并保留在8位中时,则将其视为-1。
所以你的问题是:
可以将字符串转换为ASCII值?
有点错误-字符串中的所有字符都已经是ascii值-这仅取决于您如何对待/打印/解释/显示它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句