converting wide character string to double

i want to convert a wide character string to double & i am using
wcstod() function.
But this function is giving some wague results
my code snippet
.
.
.

WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(data , L"8.5") ;
value = wcstod(buffer, &stopstring) ;

Suppose if i want to convert double precession value to wide
character string can i use swprint.
Is this format correct
swprintf(data , “%2.1lf” , value) ;
Also this is not converting to string properly.

thanx

u can do one thing convert it to multi byte character and then use the normal function for the conversion from string to double.

All the best,


What is SARS? Find out! Be informed.

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a WCHAR, you
must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
#include <iostream.h>
#include <stdio.h>

main()
{
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(buffer , L"8.5") ;
value = wcstod(buffer, &stopstring) ;

value = value + 4.0f;

cout << “Value: " << value << endl;

sprintf((char *)buffer, “%f”, value);

cout << (char *) buffer << endl;

return 0;

}==================================================

-----Original Message-----
From: unknown name [mailto:xxxxx@rediffmail.com]
Sent: Friday, April 18, 2003 8:27 AM
To: NT Developers Interest List
Subject: [ntdev] converting wide character string to double

i want to convert a wide character string to double & i am using
wcstod() function.
But this function is giving some wague results
my code snippet
.
.
.

WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(data , L"8.5”) ;
value = wcstod(buffer, &stopstring) ;

Suppose if i want to convert double precession value to wide
character string can i use swprint.
Is this format correct
swprintf(data , “%2.1lf” , value) ;
Also this is not converting to string properly.

thanx


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.</stdio.h></iostream.h></windows.h>

Why don’t we rename this email list: introduction to basic C programming?

-----Original Message-----
From: Moreira, Alberto [mailto:xxxxx@compuware.com]
Sent: Friday, April 18, 2003 11:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a
WCHAR, you must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
> #include <iostream.h>
> #include <stdio.h>
>
> main()
> {
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(buffer , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> value = value + 4.0f;
>
> cout << “Value: " << value << endl;
>
> sprintf((char *)buffer, “%f”, value);
>
> cout << (char *) buffer << endl;
>
> return 0;
>
> }==================================================
>
> -----Original Message-----
> From: unknown name [mailto:xxxxx@rediffmail.com]
> Sent: Friday, April 18, 2003 8:27 AM
> To: NT Developers Interest List
> Subject: [ntdev] converting wide character string to double
>
>
> i want to convert a wide character string to double & i am using
> wcstod() function.
> But this function is giving some wague results
> my code snippet
> .
> .
> .
>
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(data , L"8.5”) ;
> value = wcstod(buffer, &stopstring) ;
>
> Suppose if i want to convert double precession value to wide
> character string can i use swprint.
> Is this format correct
> swprintf(data , “%2.1lf” , value) ;
> Also this is not converting to string properly.
>
> thanx
>
>
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@compuware.com To unsubscribe send a blank
> email to xxxxx@lists.osr.com
>
>
>
> The contents of this e-mail are intended for the named
> addressee only. It contains information that may be
> confidential. Unless you are the named addressee or an
> authorized designee, you may not copy or use it, or disclose
> it to anyone else. If you received it in error please notify
> us immediately and then destroy it.
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@stratus.com To unsubscribe send a blank email to
> xxxxx@lists.osr.com
></stdio.h></iostream.h></windows.h>

Or use wsprintf()

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Moreira, Alberto
Sent: Friday, April 18, 2003 8:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a WCHAR, you
must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
#include <iostream.h>
#include <stdio.h>

main()
{
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(buffer , L"8.5") ;
value = wcstod(buffer, &stopstring) ;

value = value + 4.0f;

cout << “Value: " << value << endl;

sprintf((char *)buffer, “%f”, value);

cout << (char *) buffer << endl;

return 0;

}==================================================

-----Original Message-----
From: unknown name [mailto:xxxxx@rediffmail.com]
Sent: Friday, April 18, 2003 8:27 AM
To: NT Developers Interest List
Subject: [ntdev] converting wide character string to double

i want to convert a wide character string to double & i am using
wcstod() function.
But this function is giving some wague results
my code snippet
.
.
.

WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(data , L"8.5”) ;
value = wcstod(buffer, &stopstring) ;

Suppose if i want to convert double precession value to wide
character string can i use swprint.
Is this format correct
swprintf(data , “%2.1lf” , value) ;
Also this is not converting to string properly.

thanx


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only.
It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or
disclose
it to anyone else. If you received it in error please notify us
immediately
and then destroy it.


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</stdio.h></iostream.h></windows.h>

Or rather, C++. I used cout, and it’s a .cpp file. :slight_smile:

Actually, have you noticed that VC6 forces you to specifically put in the .c
extension when creating a new file ? If you don’t, it defaults to .cpp.

But seriously, to me at least this is a lot more interesting than rehashing
by the umpteenth time how IoMungeBlah works, as if RTFM wasn’t enough. Yet
both kinds of issues are relevant to NT driver development, and there’s
plenty of newbies out there !

Alberto.

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Friday, April 18, 2003 11:31 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Why don’t we rename this email list: introduction to basic C programming?

-----Original Message-----
From: Moreira, Alberto [mailto:xxxxx@compuware.com]
Sent: Friday, April 18, 2003 11:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a
WCHAR, you must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
> #include <iostream.h>
> #include <stdio.h>
>
> main()
> {
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(buffer , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> value = value + 4.0f;
>
> cout << “Value: " << value << endl;
>
> sprintf((char *)buffer, “%f”, value);
>
> cout << (char *) buffer << endl;
>
> return 0;
>
> }==================================================
>
> -----Original Message-----
> From: unknown name [mailto:xxxxx@rediffmail.com]
> Sent: Friday, April 18, 2003 8:27 AM
> To: NT Developers Interest List
> Subject: [ntdev] converting wide character string to double
>
>
> i want to convert a wide character string to double & i am using
> wcstod() function.
> But this function is giving some wague results
> my code snippet
> .
> .
> .
>
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(data , L"8.5”) ;
> value = wcstod(buffer, &stopstring) ;
>
> Suppose if i want to convert double precession value to wide
> character string can i use swprint.
> Is this format correct
> swprintf(data , “%2.1lf” , value) ;
> Also this is not converting to string properly.
>
> thanx
>
>
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@compuware.com To unsubscribe send a blank
> email to xxxxx@lists.osr.com
>
>
>
> The contents of this e-mail are intended for the named
> addressee only. It contains information that may be
> confidential. Unless you are the named addressee or an
> authorized designee, you may not copy or use it, or disclose
> it to anyone else. If you received it in error please notify
> us immediately and then destroy it.
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@stratus.com To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.</stdio.h></iostream.h></windows.h>

When things get slow, this list tends to digress… I find it quite
pleasing myself; it is a welcomed distraction sometimes :slight_smile:

Jamey

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Roddy, Mark
Sent: Friday, April 18, 2003 8:31 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Why don’t we rename this email list: introduction to basic C
programming?

-----Original Message-----
From: Moreira, Alberto [mailto:xxxxx@compuware.com]
Sent: Friday, April 18, 2003 11:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a
WCHAR, you must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
> #include <iostream.h>
> #include <stdio.h>
>
> main()
> {
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(buffer , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> value = value + 4.0f;
>
> cout << “Value: " << value << endl;
>
> sprintf((char *)buffer, “%f”, value);
>
> cout << (char *) buffer << endl;
>
> return 0;
>
> }==================================================
>
> -----Original Message-----
> From: unknown name [mailto:xxxxx@rediffmail.com]
> Sent: Friday, April 18, 2003 8:27 AM
> To: NT Developers Interest List
> Subject: [ntdev] converting wide character string to double
>
>
> i want to convert a wide character string to double & i am using
> wcstod() function.
> But this function is giving some wague results
> my code snippet
> .
> .
> .
>
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(data , L"8.5”) ;
> value = wcstod(buffer, &stopstring) ;
>
> Suppose if i want to convert double precession value to wide
> character string can i use swprint.
> Is this format correct
> swprintf(data , “%2.1lf” , value) ;
> Also this is not converting to string properly.
>
> thanx
>
>
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@compuware.com To unsubscribe send a blank
> email to xxxxx@lists.osr.com
>
>
>
> The contents of this e-mail are intended for the named
> addressee only. It contains information that may be
> confidential. Unless you are the named addressee or an
> authorized designee, you may not copy or use it, or disclose
> it to anyone else. If you received it in error please notify
> us immediately and then destroy it.
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@stratus.com To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</stdio.h></iostream.h></windows.h>

Actually, no. If you use wsprintf without the cast, VC6 bounces you with a
nasty reminder that it can’t convert an unsigned short [10] into a char *.

Alberto.

-----Original Message-----
From: Jamey Kirby [mailto:xxxxx@storagecraft.com]
Sent: Friday, April 18, 2003 11:32 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Or use wsprintf()

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Moreira, Alberto
Sent: Friday, April 18, 2003 8:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: converting wide character string to double

Works in my system under VC6. Of course, you have to say
wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.

My little program follows. Note that because your buffer is a WCHAR, you
must cast it to a (char *) if you want to use sprintf.

Alberto.

==================================================
#include <windows.h>
#include <iostream.h>
#include <stdio.h>

main()
{
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(buffer , L"8.5") ;
value = wcstod(buffer, &stopstring) ;

value = value + 4.0f;

cout << “Value: " << value << endl;

sprintf((char *)buffer, “%f”, value);

cout << (char *) buffer << endl;

return 0;

}==================================================

-----Original Message-----
From: unknown name [mailto:xxxxx@rediffmail.com]
Sent: Friday, April 18, 2003 8:27 AM
To: NT Developers Interest List
Subject: [ntdev] converting wide character string to double

i want to convert a wide character string to double & i am using
wcstod() function.
But this function is giving some wague results
my code snippet
.
.
.

WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(data , L"8.5”) ;
value = wcstod(buffer, &stopstring) ;

Suppose if i want to convert double precession value to wide
character string can i use swprint.
Is this format correct
swprintf(data , “%2.1lf” , value) ;
Also this is not converting to string properly.

thanx


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only.
It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or
disclose
it to anyone else. If you received it in error please notify us
immediately
and then destroy it.


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.</stdio.h></iostream.h></windows.h>

“Moreira, Alberto” wrote in message
news:xxxxx@ntdev…
>
> Works in my system under VC6. Of course, you have to say
> wcscpy(buffer,L"8.5"), it’s “buffer” and not “data”.
>
> My little program follows. Note that because your buffer is a WCHAR, you
> must cast it to a (char *) if you want to use sprintf.
>
> Alberto.
>
>
> ==================================================
> #include <windows.h>
> #include <iostream.h>
> #include <stdio.h>
>
> main()
> {
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(buffer , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> value = value + 4.0f;
>
> cout << "Value: " << value << endl;
>
> sprintf((char *)buffer, “%f”, value);
>
> cout << (char ) buffer << endl;
>
> return 0;
>

Alberto, you do know that you are intentionally breaking the type of WCHAR
to use it as an array of bytes char
, yes?

This is horribly bad practice in any code, especially code that you are
offering to a beginner, and there’s no need for it. Why do you use C++
again? Apparently, not for the type enforcement…

swprintf(buffer, “%f”, value);

Phil

Philip D. Barila
Seagate Technology, LLC
(720) 684-1842
As if I need to say it: Not speaking for Seagate.</stdio.h></iostream.h></windows.h>

> -----Original Message-----

From: Jamey Kirby [mailto:xxxxx@storagecraft.com]
Sent: Friday, April 18, 2003 8:47 AM

When things get slow, this list tends to digress… I find it quite
pleasing myself; it is a welcomed distraction sometimes :slight_smile:

Jamey

Too true! Yesterday I was ROTFL. I’m coding BIOS right now, and could use
a little levity…and a JTAG debugger…

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Roddy, Mark
Sent: Friday, April 18, 2003 8:31 AM

Why don’t we rename this email list: introduction to basic C
programming?

I could indeed have used swprintf(), but he specifically asked about
sprintf. And chars are smaller than wchar_t’s, so, no big deal. And hey,
that’s what casts are for, no ?

But I wouldn’t have coded like that, I’d have used the C++ wstring class,
and an output stream. Look, Ma, no windows.h ! No sprintf ! Even works in
Linux. :wink:

Alberto.

//============================================================
#include
#include
#include

using namespace std;

main()
{
wstring buffer(L"8.5");

wchar_t stopstring ;

double value ;

ostringstream s;

value = wcstod(buffer.data(), &stopstring) ;

s << "Value: " << value << endl;

value = value + 4.0f;

s << "New: " << value << endl;

cout << s.str() ;

return 0;
}
//=============================================================

-----Original Message-----
From: Phil Barila [mailto:xxxxx@Seagate.com]
Sent: Friday, April 18, 2003 12:14 PM
To: NT Developers Interest List
Subject: [ntdev] Re: converting wide character string to double

Alberto, you do know that you are intentionally breaking the type of WCHAR
to use it as an array of bytes char
, yes?

This is horribly bad practice in any code, especially code that you are
offering to a beginner, and there’s no need for it. Why do you use C++
again? Apparently, not for the type enforcement…

swprintf(buffer, “%f”, value);

Phil

Philip D. Barila
Seagate Technology, LLC
(720) 684-1842
As if I need to say it: Not speaking for Seagate.


You are currently subscribed to ntdev as: xxxxx@compuware.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it.

> This is horribly bad practice in any code, especially code that you are

offering to a beginner, and there’s no need for it. Why do you use C++
again? Apparently, not for the type enforcement…

swprintf(buffer, “%f”, value);

To be precise, it should be:

swprintf(buffer, L"%f", value);

Alternatively, if we want to do it the type-safe C++ way:

#include
#include
#include
#include

int
main()
{
using namespace std;

wchar_t buffer[10];
wchar_t *stopstring;
double value;

wcscpy(buffer , L"8.5") ;
wcout << L"old buffer: " << buffer << endl;

value = wcstod(buffer, &stopstring) ;
value = value + 4;

wstringstream strm;
strm << value;
wcscpy(buffer, strm.str().c_str());

wcout << L"new buffer: " << buffer << endl;

return 0;
}

Isn’t this fun?

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Faris Y. Yau
Sent: Friday, April 18, 2003 10:02 AM
To: NT Developers Interest List
Subject: [ntdev] Re: converting wide character string to double

This is horribly bad practice in any code, especially code that you
are
offering to a beginner, and there’s no need for it. Why do you use
C++
again? Apparently, not for the type enforcement…

swprintf(buffer, “%f”, value);

To be precise, it should be:

swprintf(buffer, L"%f", value);

Alternatively, if we want to do it the type-safe C++ way:

#include
#include
#include
#include

int
main()
{
using namespace std;

wchar_t buffer[10];
wchar_t *stopstring;
double value;

wcscpy(buffer , L"8.5") ;
wcout << L"old buffer: " << buffer << endl;

value = wcstod(buffer, &stopstring) ;
value = value + 4;

wstringstream strm;
strm << value;
wcscpy(buffer, strm.str().c_str());

wcout << L"new buffer: " << buffer << endl;

return 0;
}


You are currently subscribed to ntdev as: xxxxx@storagecraft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Terrible, just terrible.

I am assuming here that you defined “data” and used “buffer” but they are
one and the same? So your fragment should read:

WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(buffer , L"8.5") ;
value = wcstod(buffer, &stopstring) ;

Code fragments used here many times reflect real code, with “the names
changed to protect your NDA.” Given that, I suspect that the L"8.5" could
very possibly be a dynamic variable input to a function you are using to do
this conversion. You do realize that you have left yourself wide open to a
static
buffer overlfow? Defining buffer as a WCHAR on the stack may or not be bad,
but at no time do you make sure that you are not going to overflow your
buffer.
Let’s say your code really looks like this:

VOID MyFunction(WCHAR inString) {
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value ;

wcscpy(buffer , inString) ;
value = wcstod(buffer, &stopstring) ;
return;
}

You are at the mercy of inString to have a terminator, but if the dork
calling your function didn’t terminate his string, it will appear to be your
function that has failed. If the dork is of the malicious variety he can
easily determine the proper size of inString, overflow buffer and insert his
own return address on the stack. When MyFunction returns, control passes to
the dork’s function and you have been summarily “cracked”.

There are several ways to secure your code from such problems. The way I
suggest is to look for SRTSAFE.H for applications and/or NTSTRSAFE.H for
drivers and use the functions described there. Your code would then appear
like the following fragment written for a kernel driver:

VOID MyFunction(WCHAR inString) {
WCHAR buffer[10] ;
wchar_t *stopstring ;
double value;
NTSTATUS status;

status = RtlStringCbCopyW(buffer , inString) ;
if (NT_SUCCESS(status)) {
value = wcstod(buffer, &stopstring);
return;
}


Gary G. Little
Have Computer, Will Travel …
909-698-3191
909-551-2105
http://www.wd-3.com

“unknown name” wrote in message
news:xxxxx@ntdev…
>
> i want to convert a wide character string to double & i am using
> wcstod() function.
> But this function is giving some wague results
> my code snippet
> .
> .
> .
>
> WCHAR buffer[10] ;
> wchar_t *stopstring ;
> double value ;
>
> wcscpy(data , L"8.5") ;
> value = wcstod(buffer, &stopstring) ;
>
> Suppose if i want to convert double precession value to wide
> character string can i use swprint.
> Is this format correct
> swprintf(data , “%2.1lf” , value) ;
> Also this is not converting to string properly.
>
> thanx
>
>
>
>
>
>