Elpezmuerto
Elpezmuerto

Reputation: 5581

How to convert char* to unsigned short in C++

I have a char* name which is a string representation of the short I want, such as "15" and need to output this as unsigned short unitId to a binary file. This cast must also be cross-platform compatible.

Is this the correct cast: unitId = unsigned short(temp);

Please note that I am at an beginner level in understanding binary.

Upvotes: 9

Views: 58978

Answers (6)

JSBձոգչ
JSBձոգչ

Reputation: 41388

I assume that your char* name contains a string representation of the short that you want, i.e. "15".

Do not cast a char* directly to a non-pointer type. Casts in C don't actually change the data at all (with a few exceptions)--they just inform the compiler that you want to treat one type into another type. If you cast a char* to an unsigned short, you'll be taking the value of the pointer (which has nothing to do with the contents), chopping off everything that doesn't fit into a short, and then throwing away the rest. This is absolutely not what you want.

Instead use the std::strtoul function, which parses a string and gives you back the equivalent number:

unsigned short number = (unsigned short) strtoul(name, NULL, 0);

(You still need to use a cast, because strtoul returns an unsigned long. This cast is between two different integer types, however, and so is valid. The worst that can happen is that the number inside name is too big to fit into a short--a situation that you can check for elsewhere.)

Upvotes: 19

Nick Smith
Nick Smith

Reputation: 146

If you have a string (char* in C) representation of a number you must use the appropriate function to convert that string to the numeric value it represents.

There are several functions for doing this. They are documented here: http://www.cplusplus.com/reference/clibrary/cstdlib

Upvotes: 0

Amardeep AC9MF
Amardeep AC9MF

Reputation: 19064

To convert a string to binary in C++ you can use stringstream.

#include <sstream>

. . .

int somefunction()
{
    unsigned short num;
    char *name = "123";
    std::stringstream ss(name);

    ss >> num;

    if (ss.fail() == false)
    {
        // You can write out the binary value of num.  Since you mention
        // cross platform in your question, be sure to enforce a byte order.
    }
}

Upvotes: 6

Tim Schaeffer
Tim Schaeffer

Reputation: 2636

Is the pointer name the id, or the string of chars pointed to by name? That is if name contains "1234", do you need to output 1234 to the file? I will assume this is the case, since the other case, which you would do with unitId = unsigned short(name), is certainly wrong.

What you want then is the strtoul() function.

char * endp
unitId = (unsigned short)strtoul(name, &endp, 0);
if (endp == name) {
     /* The conversion failed. The string pointed to by name does not look like a number. */
}

Be careful about writing binary values to a file; the result of doing the obvious thing may work now but will likely not be portable.

Upvotes: 0

fredoverflow
fredoverflow

Reputation: 263350

#include <boost/lexical_cast.hpp>

unitId = boost::lexical_cast<unsigned short>(temp);

Upvotes: 10

Chris Card
Chris Card

Reputation: 3266

that cast will give you (a truncated) integer version of the pointer, assuming temp is also a char*. This is almost certainly not what you want (and the syntax is wrong too). Take a look at the function atoi, it may be what you need, e.g. unitId = (unsigned short)(atoi(temp)); Note that this assumes that (a) temp is pointing to a string of digits and (b) the digits represent a number that can fit into an unsigned short

Upvotes: 2

Related Questions