convert BSTR to a long

T

tojo

I have a BSTR with the string "123". I need this as a long (or UINT).
How can I do this? I am a newbie, does it show?

Thanks,
Tom
 
J

John Harrison

tojo said:
I have a BSTR with the string "123". I need this as a long (or UINT).
How can I do this? I am a newbie, does it show?

Thanks,
Tom

Loop though the string one character at a time, each time round the loop
multiply the value you have so far by 10, and add the value of the latest
digit.

I.e. in pseudo code

long val = 0;
while (more digits)
{
val *= 10;
val += next digit;
}

In your example val will go like this

val = 0;
val = 0; // times 10
val = 1; // plus digit '1'
val = 10; // times 10
val = 12; // plus digit '2'
val = 120; // times 10
val = 123; // plus digit '3'

Get the picture?

john
 
S

Stephen Howe

I have a BSTR with the string "123". I need this as a long (or UINT).
How can I do this? I am a newbie, does it show?

It does indeed.

Questions on standard C++ should be asked here.

Questions on Microsoft technology (like BSTR) should be asked on Microsofts
news server: msnews.microsoft.com. There are 22+ newsgroups devoted to VC++
alone.

Stephen Howe
 
T

Tom Lee

tojo said:
I have a BSTR with the string "123". I need this as a long (or UINT).
How can I do this? I am a newbie, does it show?

Thanks,
Tom

First, you'll need to convert the BSTR to an ordinary sequence of
characters (wcstombs I think should do it for you - the docs are your
friend).

Then just use atoi/atol to convert it.

Good luck.
Tom L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top