Search code examples
c++stringraspberry-pi3uint8t

uint8_t to String, leads to no output C++ (Beginner programer)


I'm a beginner at programming and I'm trying to build a program for raspberry pi in C++, I started with a program that ouput the UID on the console which was this

for(byte i = 0; i < mfrc.uid.size; ++i)
{
    if(mfrc.uid.uidByte[i] < 0x10)
    {
        printf(" 0");
        printf("%X",mfrc.uid.uidByte[i]);
    }
    else
    {
        printf(" ");
        printf("%X", mfrc.uid.uidByte[i]);
    }
}

now I wanted to change that to output a string which can be called by another program instead. So I changed the code to

stringstream list;
for(byte i = 0; i < mfrc.uid.size;++i)
{
    list << (int)mfrc.uid.uidByte[i];
}
string s = list.str();
cout << s;

it compiles fine however the program does not cout anything, perhaps I am taking a wrong approach, I've looked around stackoverflow for previously asked question but I can't seem to find something that I comprehend! haha, thanks for the help


Solution

  • You wrote:

    stringstream list;
    

    So I suspect you have an using namespace std; somewhere above. The thing is, std::list exists and is a type. In the remaining part of your program, when you write list, it might be std::list which is found instead. I don't know how it plays out, but I'm confident this is not what you think.

    This is why using namespace std is considered bad practice. Dont.