stream - stringstream >> uint8_t in hex? c++ -
i confused output of following code:
uint8_t x = 0, y = 0x4a; std::stringstream ss; std::string = "4a"; ss << std::hex << a; ss >> x; std::cout << (int)x << " "<< (int)y << std::endl; std::cout << x << " "<< y <<std::endl; std::cout << std::hex << (int)x << " " << (int)y << std::endl; uint8_t z(x); std::cout << z;
the output above is:
52 74
4 j
34 4a
4
and when change replace first line with:
uint16_t x = 0, y = 0x4a;
the output turns into:
74 74
74 74
4a 4a
j
i think understand happens don't understand why happens or how can prevent it/work around it. understanding std::hex
modifier somehow undermined because of type of x
, maybe not true @ technical level writes first character reads.
background: input supposed string of hexadecimal digits, each pair representing byte( bitmap except in string). want able read each byte , store in uint8_t
experimenting when came across problem. still can't determine what's best method of if think i'm doing inefficient or unnecessary appreciate know why. thank reading,
ss >> x
is treating uint8_t x unsigned char. ascii value of '4' (decimal) 52. it's reading first char of string "4a" x if x character. when switch uint16_t, it's treating unsigned short integer type. same y.
Comments
Post a Comment