c++ - Convert MSB first to LSB first -
i want convert unsigned short value msb first lsb first. did below code not working. can point error did
#include <iostream> using namespace std; int main() { unsigned short value = 0x000a; char *m_pcurrent = (char *)&value; short temp; temp = *(m_pcurrent+1); temp = (temp << 8) | *(unsigned char *)m_pcurrent; m_pcurrent += sizeof(short); cout << "temp " << temp << endl; return 0; }
what wrong first assigned value
's msb temp
's lsb, shifted again msb , assigned value
's lsb lsb. basically, had interchanged *(m_pcurrent + 1)
, *m_pcurrent
whole thing had no effect.
the simplified code:
#include <iostream> using namespace std; int main() { unsigned short value = 0x00ff; short temp = ((char*) &value)[0]; // assign value's lsb temp = (temp << 8) | ((char*) &value)[1]; // shift lsb msb , add value's msb cout << "temp " << temp << endl; return 0; }
Comments
Post a Comment