1

    std::stringstream sstream;
    uint8_t data[1] = { };
    int x =1;
    sstream << "0x" << std::hex << x; //sstream = 0x04b35cc8 "0x1" why the sstream not be only "0x1"    
    uint8_t result= (uint8_t)sstream.str().c_str();// result= 80 
    data[0] = { result};

the function that I want to implement do:

1- convert to hex. For example 1 converted to 0x01 ( Is there another way rather than using stringstream)

2- store the result in uint8_t variable in order to assign it to data[0]

user216905
  • 31
  • 1
  • 7
  • 1
    Integers do not care about 'being in hex', that's purely a display format – perivesta Jan 13 '22 at 09:43
  • The string in the `sstream` is `"0x1"`. You're converting a pointer to the string's first element into `uint8_t`. – molbdnilo Jan 13 '22 at 09:43
  • 1
    It's very unclear what your goal is - the literal `1` denotes the same number as the literal `0x1` (that is, the number one). Numbers themselves are not decimal or hexadecimal or anything else. – molbdnilo Jan 13 '22 at 09:45
  • I want to convert any integer to hex format ex. 12 to be C in hex – user216905 Jan 13 '22 at 09:49
  • That only makes sense if you convert to a string. I think you're confusing the integers with their spelling in various writing systems. (`sstream.str()` is the hexadecimal representation of the number one. You can convert this back to a number by streaming from `sstream` with `>>`. This would give you the same number that you started with, making the whole "conversion" pointless.) – molbdnilo Jan 13 '22 at 09:55

0 Answers0