Convert binary string to decimal, strtol()

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!!!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
if (dnum >= 512)
   dnum -= 1024 ;
DPhoenix121Author Commented:
Excellent, worked like a charm.  Thanks chip3d
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.