How to split a word into two bytes in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

union Word {
	short s; 
	struct TwoByte {
		char ch1, ch2;  
	} bytes;
};

int main()
{
	Word w;
	w.s = 0x3F10;

	cout << (int)w.bytes.ch1 << endl;
	cout << (int)w.bytes.ch2 << endl;

	return 0;
}


/*
run:

16
63

*/

 



answered May 29, 2018 by avibootz

Related questions

...