Converting Short Int to Bytes and vice versa.

Posted on 2011-05-03
Medium Priority
Last Modified: 2012-05-11
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.
Question by:Mydeen Yussouf
LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 35515632
>>             k |= ((UINT16)buf[0]) << 8;

There seems to be an extra | in this line ...
LVL 53

Expert Comment

ID: 35515635
Or in other words, you probably meant :

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

Author Comment

by:Mydeen Yussouf
ID: 35515864
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)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 53

Expert Comment

ID: 35516530
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;
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35564563
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).


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question