简体   繁体   English

将char 2d数组转换为int 2d数组

[英]Converting char 2d array to int 2d array

I am trying to convert a char of 2D array to int of 2D array so that I can get ASCII value of each character but whenever I print the output I am getting output in Hexadecimal.But when I tried to convert that hexadecimal to ASCII the value does not equal to any of the ASCII Values 我正在尝试将2D数组的char转换为2D数组的int,以便可以获取每个字符的ASCII值,但是每当我打印输出时,我都会以十六进制输出。但是当我尝试将十六进制转换为ASCII时,该值不等于任何ASCII值

#include <iostream>
using namespace std;
int main() {
    int n;
    cout<<"Enter input"<<endl;
    cin>>n;
    char a [n][50];
   int arr [n][50];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        arr[i][50]=a[i][50];
    }
    int count=0;
    for(int i=0;i<n;i++)
     {
       cout<<arr[i];
       cout<<endl;
     }
return 0;
}

I accept the output 65 but the output I am getting is 0x7ffee224f940. 我接受输出65,但我得到的输出是0x7ffee224f940。 Input that I gave was A 我输入的是A

You are trying to print an integer array. 您正在尝试打印一个整数数组。 The hexadecimal output that you are getting is the address of the array, not the contents. 您获得的十六进制输出是数组的地址,而不是内容。 If you want to get the value of a single character, you should print them one by one. 如果要获取单个字符的值,则应一张一张打印。

There are 2 major problems with your code. 您的代码有2个主要问题。

The first is this part right here: 第一部分是这里的部​​分:

    int n;
    cout<<"Enter input"<<endl;
    cin>>n;
    char a [n][50];
    int arr [n][50];

Since n is not known at compile time, char a [n][50]; 由于n在编译时未知,因此char a [n][50]; and int arr [n][50]; int arr [n][50]; are two really bad things you shouldn't do. 这是您不应该做的两件事。 Some compilers might let you get away with this, but in general, array declarations need to know the size ahead of time. 一些编译器可能会让您难以理解,但是通常,数组声明需要提前知道大小。 For some examples of how to do this the right way, see the answers to this question . 有关如何正确执行此操作的一些示例, 请参见此问题的答案

Problem 2: 问题2:

arr[i][50]=a[i][50];

Another very bad thing you're doing here. 您在这里所做的另一件非常糟糕的事情。 Array subscripts are 0 based , meaning that in your example, there is no such thing as arr[i][50] . 数组下标是从0开始的 ,这意味着在您的示例中,没有arr[i][50]这样的东西。 The last one is arr[i][49] . 最后一个是arr[i][49] The linked question gives some good reasons why it is 0 based, so I won't go over it again. 链接的问题给出了一些很好的理由,为什么它基于0,所以我不再赘述。 Just know that because the first element of the array starts at arr[i][0] , you actually have to access 1 less than your original size to get the last element of the array. 只需知道,由于数组的第一个元素以arr[i][0]开头,因此您实际上必须比原始大小少访问1才能获取数组的最后一个元素。

Failure to follow these guidelines can lead to unexpected results at the very least, and crashing at the worst, as you have found out. 如您所知,不遵循这些准则至少会导致意外结果,而在最坏情况下会崩溃。

Though some complilers supports its own language extensions nevertheless variable length arrays 尽管某些编译器支持其自己的语言扩展,但是可变长度数组

cin>>n;
char a [n][50];
int arr [n][50];

is not a standard C++ feature. 不是标准的C ++功能。 So instead them it is better to use the standard container std::vector . 因此,相反,最好使用标准容器std::vector

It seems in this statement 看来这句话

arr[i][50]=a[i][50];

you are trying to assign one array to another array. 您正在尝试将一个数组分配给另一个数组。 Apart from the expressions are incorrect arrays do not have the assignment operator. 除了表达式之外,不正确的数组没有赋值运算符。

Below is a demonstrative program that shows how to perform your task. 下面是一个演示程序,显示了如何执行任务。

#include <iostream>
#include <string>
#include <limits>
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstring>

int main() 
{
    const size_t N = 50;

    std::cout << "Enter the number of strings: ";

    size_t n;

    std::cin >> n;

    std::vector<std::array<char, N>> strings( n );
    std::vector<std::array<int, N>> values( n );

    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        std::cin.getline( strings[i].data(), N, '\n' );
    }

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        std::cout << strings[i].data() << '\n';
    }

    for ( size_t i = 0; i < strings.size(); i++ )
    {
        size_t len = std::strlen( strings[i].data() ) + 1;
        std::copy( strings[i].data(), strings[i].data() + len, std::begin( values[i] ) );       
    }

    for ( const auto &row : values )
    {
        for ( auto it = std::begin( row ); *it != 0; ++it )
        {
            std::cout << *it << ' ';
        }
        std::cout << '\n';
    }

    return 0;
}

The program output might look the following way 程序输出可能如下所示

Enter the number of strings: 2
Hello
World
72 101 108 108 111 
87 111 114 108 100 

Let's delve into your code step by step: 让我们逐步研究代码:

  1. To input a two dimensional array you need two dimensions, ie generally two variables are used to define a single element in the array.Thus requiring nested for loop, a single for loop wont suffice. 要输入二维数组,您需要二维,即通常使用两个变量来定义数组中的单个元素。因此,需要嵌套的for循环,一个for循环就足够了。

  2. The array indices begin from 0, thus the last element of an array can only be n-1, or here 50-1=49. 数组索引从0开始,因此数组的最后一个元素只能是n-1,或者在这里50-1 = 49。 Thus pointing to a variable location indexed at 50 is wrong too. 因此,指向索引为50的可变位置也是错误的。

  3. The assignment arr[i][j]=a[i][j], would be correct, and enough to convert character to integer. 分配arr [i] [j] = a [i] [j]是正确的,足以将字符转换为整数。

Thus Updated Code: 从而更新了代码:

#include <iostream>
using namespace std;
int main() 
{
    int n;
    cout<<"Enter input"<<endl;
    cin>>n;

    //The 50 in the second parameter, implies that the column width will always be 50
    char a[n][50];
    int arr[n][50];

    for(int i=0;i<n;i++)
    {
        for(j=0;j<50;j++)
        {
            cin>>a[i][j];
            //Direct assignment to obtain the ascii value
            arr[i][j]=int(a[i][j]);
        }
    }


    for(int i=0;i<n;i++)
    {
        for(j=0;j<50;j++)
        {
            cout<<arr[i];
            cout<<endl;
        }
    }

return 0;
}

You can know more about 2D arrays here 您可以在此处了解有关2D阵列的更多信息

<--Upvote if Helpful--> <-如果有帮助则进行投票->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM