字符串转ascii码c语言
时间: 2023-12-19 15:06:47 浏览: 102
可以使用C语言中的toascii()函数将字符串转换为ASCII码。下面是一个简单的例子:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello World!";
int i;
for(i = 0; str[i]; i++) {
printf("ASCII code of %c is %d\n", str[i], toascii(str[i]));
}
return 0;
}
```
运行结果如下:
```
ASCII code of H is 72
ASCII code of e is 101
ASCII code of l is 108
ASCII code of l is 108
ASCII code of o is 111
ASCII code of is 32
ASCII code of W is 87
ASCII code of o is 111
ASCII code of r is 114
ASCII code of l is 108
ASCII code of d is 100
ASCII code of ! is 33
```
相关问题
字符串转ascii码 c语言
可以使用C语言中的`printf()`函数来将字符串转换为ASCII码。具体实现如下:
```c
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
for(int i = 0; str[i] != '\0'; i++) {
printf("%d ", str[i]);
}
return 0;
}
```
输出结果为:
```
72 101 108 108 111 44 32 119 111 114 108 100 33
```
其中,`%d`表示输出整数,`str[i]`表示字符串中第i个字符。循环遍历字符串,每次输出字符对应的ASCII码即可。
c语言字符串转ascii码
好的,对于您的问题,我可以回答。c语言中,我们可以使用ASCII码来表示字符串中的字符。每个字符都有一个对应的ASCII码值,可以使用C语言中的函数来转换。
您可以使用C语言中的函数“int printf(const char *format, ...);”来把字符串转换成ASCII码。例如,下面的程序将字符串“Hello, World!”转换成ASCII码:
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int i;
for(i = 0; str[i] != '\0'; i++) {
printf("%d ", str[i]);
}
return 0;
}
输出结果为:
72 101 108 108 111 44 32 87 111 114 108 100 33
这些数字就是ASCII码值,可以表示相应的字符。希望我的回答对您有所帮助。