-2

I have a char pointer that holds the following information:

char* data = "22";

I want to copy it from the char into a short variable. I have the following code:

short BinarySerializer::getMessageTypeID(char* data)
{
    short* messageTypeID;
    memcpy( messageTypeID, data, sizeof( messageTypeID ) );
    return *messageTypeID;
}

I don't know why, but when I'm printing it I get: 12850.

This is how I'm printing it:

short temp = msg->messageTypeID;
cout << "Message ID is: " << temp << endl;

Thanks in advance

Zach Saucier
  • 24,871
  • 12
  • 85
  • 147
Shaul Zuarets
  • 839
  • 2
  • 10
  • 20

2 Answers2

1

Maybe you mean something from the following

#include <iostream>
#include <sstream>
#include <numeric>
#include <cstring>

struct BinarySerializer
{
    short getMessageTypeID( const char *data ) const;
    short getMessageTypeID1( const char *data ) const;
};

short BinarySerializer::getMessageTypeID( const char *data ) const
{
    short messageTypeID = 0;
    std::istringstream is( data );

    is >> messageTypeID;

    return messageTypeID;
}

short BinarySerializer::getMessageTypeID1( const char *data ) const
{
    short messageTypeID;

    messageTypeID = std::accumulate( data, data + std::strlen( data ), ( short)0,
                                     []( short acc, char c ) 
                                     { 
                                        return 10 * acc + ( c - '0' ); 
                                     } );

    return messageTypeID;
}

int main()
{
    const char *s = "22";

    std::cout << BinarySerializer().getMessageTypeID( s ) << std::endl;
    std::cout << BinarySerializer().getMessageTypeID1( s ) << std::endl;
}

The output is

22
22

If the data can look like "22sdfsd". then you need to add code that will find the first non-digit value.

Also you could use function std::strtol and check whether the obtained value is in the range for values of type short.

For example (without checking whether the obtained value is valid for type short)

short BinarySerializer::getMessageTypeID( const char *data ) const
{
    short messageTypeID = 0;

    messageTypeID = ( short )strtol( data, nullptr, 10 );   

    return messageTypeID;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Let's take a look at the memory, assuming size of short is twice the size of char (not always the case!):

char array with "22" string is in memory:

50 50

so basically

00110010 00110010

when you memcpy it to a short, you get a single number, consisting of two bytes:

00110010 00110010

which, translated to decimal, means 12850.

Now, I think you really want to convert the "22" ascii table into "22" value. In such case, you may refer to

Convert string to int C++

In case you really need support for strings like "23123dsdwdwqdwqd" (which is rarely a case), you may copy a substring of required size and work on the substring.

Keep in mind that only thing guaranteed by C++ standard when it comes to variable sizes is that

sizeof(char) <= sizeof (short) <= sizeof(int) <= sizeof(long)

So if you rely on the fact that sizeof(short) = 2 * sizeof(char) then you're not portable at all.

Community
  • 1
  • 1
Tomasz Lewowski
  • 1,935
  • 21
  • 29