• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

Converting Short Int to Bytes and vice versa.

I am using the attached functions to convert Int/ShortInt to BYTEs ad vice versa.

The conversion from/to Int to BYTE works well whether I use it in a standalone application or over sockets. (convert to BYTEs in a socket client and then reverse it in a socket server).

But the ShortInToByte/ByteTOShortInt works well in a stand alone application, but when I send it over sockets it is always garbled. I do not get the correct digits on the other end.

What is thisfunction missing?
//convert Int <---> bytes
	inline static void BytesToInt(int& k, uint8_t* buf) {
		k = ((int)buf[0]) << 24;
		k |= ((int)buf[1]) << 16;
		k |= ((int)buf[2]) << 8;
		k |= ((int)buf[3]);
	inline void IntToBytes(uint8_t* buf, const int& i) {
		buf[0] = (int)((i >> 24) & 0xFF);
		buf[1] = (int)((i >> 16) & 0xFF);
		buf[2] = (int)((i >> 8) & 0xFF);
		buf[3] = (int)((i & 0xFF));

	//convert short int <---> bytes
	inline static void BytesToShortInt(unsigned short& k, uint8_t* buf) {
		k |= ((UINT16)buf[0]) << 8;
		k |= ((UINT16)buf[1]);
	inline void ShortIntToBytes(uint8_t* buf, const unsigned short& i) {
		buf[0] = (UINT16)((i >> 8) & 0xFF);
		buf[1] = (UINT16)((i & 0xFF));

Open in new window

I am using Windows 7 on both ends of the socket.
ol muser
ol muser
1 Solution
>>             k |= ((UINT16)buf[0]) << 8;

There seems to be an extra | in this line ...
Or in other words, you probably meant :

            k = ((UINT16)buf[0]) << 8;
ol muserTechnology GeneralistAuthor Commented:
Not really, on the other end I do a

(UINT16)((i & 0xFF));

for all the elements of the buffer (both int as well as short int)
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Yes, but that's not really relevant to the typo you made in the BytesToShortInt function.

The |= operator does not do the same thing as the = operator.

You currently have this :

>> k |= ((UINT16)buf[0]) << 8;

which means that you take whatever value is currently in k, and perform a bitwise OR with the value on the right-hand side of the operator.
The problem here is that you don't want to use the current value of k. You want to overwrite the current value of k, so you need the = operator :

        k = ((UINT16)buf[0]) << 8;
Todd GerbertIT ConsultantCommented:
Unless you specifically wanted to do this by hand for some reason...you can use htons() & htonl() (and their reverse ntohs()/nthohl) to convert a short (htons) or a long (htonl) from host byte-order (little endian under Windows/x86) to network byte-order (which is big endian).

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now