2

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

Pang
  • 9,564
  • 146
  • 81
  • 122
  • Usually it's a good idea to print a newline (`std::cout << s << std::endl`) after each line of output to ensure it's [flushed](https://www.gnu.org/software/libc/manual/html_node/Flushing-Buffers.html) (forced to be output). This shouldn't be an issue as unflushed streams [should be flushed on exit automatically](https://stackoverflow.com/questions/15911517/is-there-a-guarantee-of-stdout-auto-flush-before-exit-how-does-it-work), but it's worth trying. – hnefatl Dec 05 '18 at 17:38
  • This may it be helpful how to convert from byte to string: https://stackoverflow.com/questions/1673445/how-to-convert-unsigned-char-to-stdstring-in-c https://github.com/miguelbalboa/rfid/issues/63 – I_Al-thamary Dec 05 '18 at 17:49
  • what about changing the converting to be `String s= String((char*)mfrc.uid.uidByte);` or try to change `int` to be `char ` – I_Al-thamary Dec 05 '18 at 18:04
  • 1
    @i_th Casting an integer value to a `char *` is an almost surefire way to get undefined behaviour. Casting eg. integer 10 to a pointer will give you a pointer to memory address 10: creating a string from this will most likely result in a segfault. – hnefatl Dec 05 '18 at 18:43
  • Note the `printf()` code is outputting each byte in hex format, but the `stringstream` code is outputting each byte in decimal format instead. If you want hex, use the `std::hex` stream manipulator. Also look at `std::setfill()` and `std::setw()`, too. – Remy Lebeau Dec 05 '18 at 22:25

1 Answers1

1

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.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • It might not answer the question. If it doesn't, I'll remove it and comment instead. In fact, I'm confident hnefatl's comment should be an answer :D – YSC Dec 05 '18 at 17:42
  • Not sure if this is the root cause ([tweaked example seems to work fine](https://repl.it/@hnefatl/SunnySatisfiedHashmaps)), but definitely good advice nonetheless. I left my comment as a comment because I'd be really surprised if it fixed things: one of the links implies that'd mean the compiler OP's using is non-standards-compliant, which would be fishy :) – hnefatl Dec 05 '18 at 17:45
  • Interesting, we are taught to always use namespace std at university, I shall remember that, Thank you alot for that info. The program works now(without freezing) and it actually output something just not sure what it is, It should output 35 9F 54 2B however it output 531598443.. I am fairly confident that the problem lies that the output is in HEX and it's trying to give me integer of what ASCII wise would be 5T+? – Tommy Normandin Racine Dec 05 '18 at 17:49
  • 1
    @TommyNormandinRacine By default, integers are converted to their decimal representation for streams. To convert to hex, you will want to use the stream manipulator [`std::hex`](https://en.cppreference.com/w/cpp/io/manip/hex) – Human-Compiler Dec 05 '18 at 17:58
  • 3
    @TommyNormandinRacine There are lot and lot of things you'll learn at university that you should be doubful about. Don't forget C++ is _hard_ and requires _years of practice_ to masterise. It is the perfect tool to shoot yourself in the foot. Your teachers lack that practice, which is fine: they're not programmers, they're _teachers_. Learn as much as you can with them, and learn from [good books](https://stackoverflow.com/a/388282/5470596) and good talks (search for the cppcon channel on youtube) and good questions on SO (just lurk) ;) – YSC Dec 05 '18 at 17:59
  • 1
    Yeah, typing a korrecte english sentence is also hard apparently :S – YSC Dec 05 '18 at 18:11