Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Converting int to LPSTR

810 views
Skip to first unread message

G-Force

unread,
Apr 26, 1999, 3:00:00 AM4/26/99
to
How to do this, _iota gives an access violation with my comp, maybe
something wrong with my
settings?

G-Force

Rico Lelina

unread,
Apr 26, 1999, 3:00:00 AM4/26/99
to
The following code will give you an access violation because lp is not
pointing to an allocated memory block:

LPSTR lp;
int i = 1;
_itoa(i, lp, 10);

The correct code is:

char buf[10];
int i = 2;
_itoa(i, buf, 10);

G-Force wrote in message ...

Tomas Restrepo

unread,
Apr 26, 1999, 3:00:00 AM4/26/99
to
G-Force,

must likely you are using an uninitialized pointer. One function I use a lot is
wsprint(), like this:

int myint = 18;
char number[20];
wsprintf ( number, "%d", myint );

--
Tomas Restrepo
win...@bigfoot.com
http://members.xoom.com/trestrep/

G-Force <jaa...@usa.net> wrote in message news:e0rSwNBk#GA.261@cppssbbsa03...

James Curran

unread,
Apr 26, 1999, 3:00:00 AM4/26/99
to
Or something could be wrong with your code..... Post a selection of
would you are using it.


--
Truth,
James [MVP]
http://www.NJTheater.Com -and-
http://www.NJTheater.Com/JamesCurran

G-Force wrote in message ...

Vincent Tieleman

unread,
Apr 26, 1999, 3:00:00 AM4/26/99
to
You probably forgot to allocate the memory before you passed the pointer to
the function. Good example:

{
char buffer[20];
int i = 3445;

_itoa(i, buffer, 10);
printf( "String of integer %d (radix 10): %s\n", i, buffer );
}

Vincent

G-Force wrote in message ...
>How to do this, _iota gives an access violation with my comp, maybe
>something wrong with my
>settings?
>

>G-Force
>
>

Barry L. Wallis

unread,
Apr 27, 1999, 3:00:00 AM4/27/99
to
If you're using C++ you can try using an ostringstream:

#include <sstream>

int main()
{
int n = 12345;
std::ostringstream format;
format << n;
std::cout << format.str() << endl;
}

Obviously, this is a contrived example, but, you get the idea. format.str()
returns an std::string. If you would like a C-style string, you can say
'format.str().c_str()'. The biggest advantage to this is you have complete
type safety. If you ever need to change 'n' to a different type (e.g., from
int to long), you do not have to make any changes to your conversion code.
--

- Barry L. Wallis
- Oceanside, California, USA

0 new messages