输出字符串的ascii码
时间: 2023-09-22 21:11:59 浏览: 30
可以使用Python中的ord函数,例如:
```python
string = "Hello, World!"
for char in string:
print(ord(char))
```
输出结果:
```
72
101
108
108
111
44
32
87
111
114
108
100
33
```
相关问题
输出字符串的ascii码c++
要输出字符串中每个字符的ASCII码,可以使用循环遍历字符串,并将每个字符转换为对应的整数值。以下是一个示例:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
for (char ch : str) {
int ascii = static_cast<int>(ch);
std::cout << "ASCII of '" << ch << "' is " << ascii << std::endl;
}
return 0;
}
```
在上述示例中,我们使用了一个范围-based for 循环来遍历字符串中的每个字符。将每个字符转换为整数值可以使用类型转换 `static_cast<int>(ch)`,然后将其输出。你可以根据需要修改字符串变量`str`来输出其他字符串中字符的ASCII码。
php字符串转ascii码
您可以使用 ord() 函数将 PHP 字符串转换为 ASCII 码。该函数返回字符串第一个字符的 ASCII 码值。如果您想要将整个字符串转换为 ASCII 码,请使用一个循环来遍历字符串中的每个字符并使用 ord() 函数将其转换为 ASCII 码。下面是一个示例代码:
```php
$str = "Hello World!";
for ($i = 0; $i < strlen($str); $i++) {
echo ord($str[$i]) . " ";
}
```
输出结果为:
```
72 101 108 108 111 32 87 111 114 108 100 33
```