Question

I am working on a code that has to perform a series of operations on an array of int8_t, where each element has a number stored, ranging from 0 to 255.

int main(int argc, char *argv[]){
    int8_t v = ...  // Imagine it's an array equal to {1, 10, 100, 255}.
    int y[4];
    for(int i=0;i<3;i++)
        y[i] = v[i]*3;

    return 0;
}

What I'm looking for is either a way to make operations with the int8_t array, or preferably a way to convert int8_t into ints (I have a big code which works with int inputs, converting int8_t to int and then giving it to my code would require less changes than changing every int to int8_t).

It may also be worth noting that the int8_t array comes from a video capture of a camera, which returns the value of each pixel from 0 to 255 in int8_t form, which means I can't change the input array.

Any help will be highly appreciated.

Edit:

Dummy code, which hopefully will help illustrate my problem.

int main(int argc, char *argv[]){

    int8_t v[4] = {'0','1','2','3'};

    int y;

    y = (int) v[1]*3;

    std::cout << v[1] << "  " << y << std::endl;

    return 0;
}

The output I get when I run this is 1 49.

Était-ce utile?

La solution 2

Your latest edit is incorrectly initializing the array.

int main(int argc, char *argv[]){

    int8_t v[4] = {'0','1','2','3'}; // <- This is wrong!

    int y;

    y = (int) v[1]*3;

    std::cout << v[1] << "  " << y << std::endl;

    return 0;
}

Numbers in single quotes are ASCII characters whose numeric values don't match the actual number in the quotes.

Please modify your code like this and see if it helps:

int main(int argc, char *argv[]){

    int8_t v[4] = {0, 1, 2, 3}; // <- Remove the single quotes

    int y;

    y = (int) v[1]*3;

    std::cout << v[1] << "  " << y << std::endl;

    return 0;
}

Here is a link to ideone

Follow up question (from comments):

I never declare the actual array, I import it directly from a .txt file. When I do this I get the same result as if I had declared it with the quotes. Is there a way to work around this, converting it from ascii code to the desired numeric value?

Yes. You can use the atoi or stoi functions to convert a number that is represented as text to an integer. You should also take a look at this link discussing how to use stream operators to achieve the same.

However, converting the numbers from strings is pretty easy so I'll give you an example:

#include <iostream>
#include <string.h>

int getNumberFromString(const char* numberString)
{
    int number = 0;
    int i;
    int stringLength = strlen(numberString);

    for (i = 0; i < stringLength; ++i)
    {
        number *= 10;
        number += numberString[i] - '0';
    }

    return number;
}

int main()
{
    const char* numberSeventyNineString = "79"; // Equivalent to { '7', '9', '\0' }
    int numberSeventyNine;

    numberSeventyNine = getNumberFromString(numberSeventyNineString);

    std::cout << "Number (" << numberSeventyNineString << ") as int is: " << numberSeventyNine << std::endl;

    return 0;
}

And here's the ideone link

Note: The line const char* numberSeventyNineString = "79"; // Equivalent to { '7', '9', '\0' } has a comment saying that this is equivalent to an array, for a little more detail please see this answer

Autres conseils

In C/C++ types that are less than int are automatically promoted to int. But int8_t is a signed type so it cannot store values such as 255 in your first snippet

For the second snippet

int8_t v[4] = {'0','1','2','3'};

'0' = 0x30 = 48 because that's its ASCII value, so multiplying it by 3 will not result in 0 like you expected

To convert a char value to its numeric value just subtract '0' from it, like v[i] - '0'

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top