Converting Short Int to Bytes and vice versa.

Posted on 2011-05-03
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:olmuser
    LVL 53

    Accepted Solution

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

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

    Expert Comment

    Or in other words, you probably meant :

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

    Author Comment

    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)
    LVL 53

    Expert Comment

    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
    Unless you specifically wanted to do this by hand for some 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).
    LVL 6

    Expert Comment


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    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…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    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.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now