2

I wrote a basic code in C++ which needs to convert two elements of vector array into string. I am not an expert in C++, i have experience in C. So, please bear my mistakes

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    std::vector<uint8_t> eventParameters  = {1, 2, 3, 4, 5, 6, 7,8 };
    std::stringstream stream;
    stream << std::hex << eventParameters[5] << eventParameters[6];
    std::string result( stream.str() );
    std::cout << result;
    return 0;
}

When i run this code i am getting garbage data displayed, but when i change to

stream << std::hex << int(eventParameters[5]) << int(eventParameters[6]);

It works. Is this approach correct. Do you see any mistake in the code

md.jamal
  • 4,067
  • 8
  • 45
  • 108

0 Answers0