Convert binary string to decimal, strtol()

Posted on 2006-04-10
Last Modified: 2012-05-05
I have a program that receives a string in binary format.  The string is a signed binary number between -511 and 512.  I need to convert the string into an int to perform some mathmatical operations on it.  I used the strtol function to convert the string, but it always treats the 10 binary digits as unsigned.  How do I convert the binary string so that it is signed?

All strings are 10 characters long, consisting of only a 1 or a 0. ie. "1111111111" should be converted to -1, not 1023.

I have
int conv (string bnum) {
     int dnum = strtol(bnum.c_str(),&pEnd,2);
     return dnum;

But it's not working, HELP!!!
Question by:DPhoenix121
    LVL 4

    Accepted Solution

    this will do the work:

    int conv (const char *str)
         char *end = 0;
         short dnum = strtol(str,&end,2);
         if (dnum & 0x200) dnum |= 0xfc00;
         return dnum;

    LVL 4

    Expert Comment

    strtol will convert your string to a 32 bit value:
    "1111111111" -> 00000000000000000000001111111111 -> 1023

    you need:
    11111111111111111111111111111111 -> -1

    bit 10 of your string indicates whether you have a signed or unsigned value:
    with the mask 0x200 you can check if this bit is set or not
    if (dnum & 0x200)

    if so, you have to overwrite all the zeros with ones
    dnum |= 0xfffffc00
    LVL 2

    Expert Comment

    if (dnum >= 512)
       dnum -= 1024 ;

    Author Comment

    Excellent, worked like a charm.  Thanks chip3d

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    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…

    758 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

    11 Experts available now in Live!

    Get 1:1 Help Now