C++ 扫描字符串每个字符的 ASCII 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13412206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:16:09  来源:igfitidea点击:

Scanning ASCII value of each character of a string

c++cstringascii

提问by Kamal Kafkaesque

Is there anyway , if I enter any string , then I want to scan ASCII value of each character inside that string , if I enter "john" then I should get 4 variables getting ASCII value of each character, in C or C++

无论如何,如果我输入任何字符串,那么我想扫描该字符串中每个字符的 ASCII 值,如果我输入“john”,那么我应该得到 4 个变量来获取每个字符的 ASCII 值,在 C 或 C++ 中

采纳答案by Omkant

yeah it's very easy ..just a demo

是的,这很容易..只是一个演示

int main()
{
 char *s="hello";
 while(*s!='
char s[] = "john";
') { printf("%c --> %d\n",*s,*s); s++; } return 0; }

But make sure your machine is supporting the ASCII value format. In C every char has one integral value associted with it called ASCII. Using %dformat specifier you can directly print the ASCII of any char as above.

但请确保您的机器支持 ASCII 值格式。在 C 中,每个字符都有一个与之相关的整数值,称为 ASCII。使用%d格式说明符,您可以直接打印上述任何字符的 ASCII。

NOTE: It's better to get good book and practice this kind of program yourself.

注意:最好自己找一本好书并练习这种程序。

回答by Mike Seymour

Given a string in C:

给定一个 C 中的字符串:

std::string s = "john";

or in C++:

或在 C++ 中:

printf("%d", s[0]);                     // in C
std::cout << static_cast<int>(s[0]);    // in C++

s[0]gives the numeric value of the first character, s[1]the second an so on.

s[0]给出第一个字符的数值,s[1]第二个以此类推。

If your computer uses an ASCII representation of characters (which it does, unless it's something very unusual), then these values are the ASCII codes. You can display these values numerically:

如果您的计算机使用字符的 ASCII 表示(它确实如此,除非它非常不寻常),那么这些值就是 ASCII 代码。您可以数字显示这些值:

for (char const * p = s; *p; ++p) {
    // Do something with the character value *p
}

Being an integer type (char), you can also assign these values to variables and perform arithmetic on them, if that's what you want.

作为整数类型 ( char),您还可以将这些值分配给变量并对它们执行算术运算,如果这是您想要的。

I'm not quite sure what you mean by "scan". If you're asking how to iterate over the string to process each character in turn, then in C it's:

我不太确定你所说的“扫描”是什么意思。如果您问如何遍历字符串以依次处理每个字符,那么在 C 中它是:

for (char c : s) {
    // Do something with the character value c
}

and in (modern) C++:

在(现代)C++ 中:

char s[SOME_SIZE_YOU_HOPE_IS_LARGE_ENOUGH];
fgets(s, sizeof s, stdin);

If you're asking how to read the string as a line of input from the terminal, then in C it's

如果你问如何从终端读取字符串作为一行输入,那么在 C 中它是

std::string s;
std::cin >> s;  // if you want a single word
std::getline(std::cin, s); // if you want a whole line

and in C++ it's

在 C++ 中它是

char c = 'b';
int i = c; //i contains ascii value of char 'b'

If you mean something else by "scan", then please clarify.

如果您的“扫描”是指其他意思,请澄清。

回答by john

There is no way to turn a string of length 'x' into x variables. In C or C++ you can only declare a fixed number of variables. But probably you don't need to do what you are saying. Perhaps you just need an array, or most likely you just need a better way to solve whatever problem you are trying to solve. If you explain what the problem is in the first place, then I'm sure a better way can be explained.

无法将长度为 'x' 的字符串转换为 x 变量。在 C 或 C++ 中,您只能声明固定数量的变量。但可能你不需要做你所说的。也许您只需要一个数组,或者很可能您只需要一种更好的方法来解决您要解决的任何问题。如果您首先解释问题是什么,那么我相信可以解释更好的方法。

回答by Philipp

You can simply get the ascii value of a char by casting it to type int:

您可以通过将字符转换为 int 类型来简单地获取字符的 ascii 值:

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

int main()
{
    string text = "John";

    for (int i = 0; i < text.size(); i++)
    {
        cout << (int)text[i] << endl; //prints corresponding ascii values (one per line)
    }
}

Thus, in your example the code to get the ascii values of a string would look something like this:

因此,在您的示例中,获取字符串的 ascii 值的代码如下所示:

char c = (char)74 // c contains 'J'

To get the corresponding char from an integer representing an entry in the ascii table, you just have to cast the int back to char again:

要从表示 ascii 表中条目的整数中获取相应的 char,您只需将 int 再次转换为 char:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(){
  char s[]="abc";
  int cnt=0;
  while(1){
    if(s[cnt++]==NULL)break;
  }
  int *a=(int *)malloc(sizeof(int)*cnt);
  for(int i=0;i<cnt;i++)a[i]=s[i];
  for(int i=0;i<cnt-1;i++)printf("%d\n",a[i]);  
  return 0;
}

The code given above was written in C++ but it basically works the same way in C (and many other languages as well I guess)

上面给出的代码是用 C++ 编写的,但它在 C 中的工作方式基本相同(我猜还有许多其他语言)

回答by Nitin Khanna

Ya,I think there are some more better solutions are also available but this one also be helpful. In C

是的,我认为还有一些更好的解决方案可用,但这个也有帮助。 在 C

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s="abc";
    //int *a=new int[s.length()];
    //for(int i=0;i<s.length();i++)a[i]=s[i];
    for(int i=0;i<s.length();i++)
    cout<<(int)s[i]<<endl;
    return 0;
}

In C++

在 C++ 中

##代码##

I hope this one will be helpful..

我希望这个会有所帮助..