double <--> unicode string

This is a discussion on double <--> unicode string within the Windows Programming forums, part of the Platform Specific Boards category; i need to convert double numbers to unicode string (wchar_t *) and back. how do i do that? all default ...

  1. #1
    CS Student hartwork's Avatar
    Join Date
    Apr 2004
    Posts
    35

    Question double <--> unicode string

    i need to convert double numbers to unicode string (wchar_t *) and back. how do i do that? all default conversion functions only support ansi.

    -- double -> ansi string --
    char *_gcvt( double value, int digits, char *buffer );
    char *_fcvt( double value, int count, int *dec, int *sign );
    char *_ecvt( double value, int count, int *dec, int *sign );

    -- ansi string -> double --
    double atof( const char *string );

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    • String to double: strtod()
    • Double to string: sprintf or _snprintf()
      --
    • Wide string to double: wcstod()
    • Double to wide string: swprintf() or _snwprintf()
      --
    • TString to double: _tcstod()
    • Double to tstring: _stprintf or _sntprintf()
      --
    • StringCchPrintf()

    In <stdio.h>, <wchar.h>, <tchar.h> and <strsafe.h> respectively.

    Unlike the functions you mention, all of these are available on most windows compilers and readily understood.
    Last edited by anonytmouse; 04-05-2004 at 10:33 PM.

  3. #3
    CS Student hartwork's Avatar
    Join Date
    Apr 2004
    Posts
    35
    using print-functions i cannot get more than 9 digits after: "%.9f".
    in my case this is not enough so my current "solution" uses _gcvt, atof and these two:


    Code:
    void unicode_to_ansi( wchar_t * szInput, char * szOutput )
    {
    	UINT uLen = wcslen( szInput );
    	while( *szInput != L'\0' )
    	{
    		*( szOutput++ ) = *( szInput++ ) & 255;
    	}
    	*szOutput = '\0';
    }
    
    void ansi_to_unicode( char * szInput, wchar_t * szOutput )
    {
    	UINT uLen = strlen( szInput );
    	while( *szInput != '\0' )
    	{
    		*( szOutput++ ) = *( szInput++ );
    	}
    	*szOutput = L'\0';
    }

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> using print-functions i cannot get more than 9 digits after: "%.9f". <<

    That's weird. This seems to work for me.
    Code:
    	char buf[100];
    	double dbl = 9.423445631463345345;
    
    	sprintf(buf, "%.40f", dbl);
    	printf("%s\n", buf);
    Down converting to ansi seems to defeat the purpose of using unicode, but if you must, you should consider using the WideCharToMultibyte() and MultibyteToWideChar() functions.

    Here is another option that gives more than 9 digits.
    Code:
    	BSTR bstr = NULL;
    	double dbl = 9.423445631463345345;
    
    	VarBstrFromR8(dbl, LOCALE_USER_DEFAULT, 0, &bstr);
    
    	printf("%ls\n", bstr);
    
    	SysFreeString(bstr);

  5. #5
    CS Student hartwork's Avatar
    Join Date
    Apr 2004
    Posts
    35

    Thumbs up

    the first one seems to works! - i thought it wouldn't. i remembered i once tested it with a two-digit number but maybe it was in php or so... it works and this seems to be the best solution to me. and i won't have to break unicode this way...

    thanks for your qualified answers and for taking the time.

    ---

    i needed this for a save-and-read-config-to-ini-file class that's part of a screensaver framework i am working on right now. i'll opst a thread here when i'm done...

    if you like you can give me your real name, e-mail address and website url so i can add you to the thank-you-section!


    thanx again // bass aka hartwork <webmaster@hartwork.org>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String to Double function
    By jaro in forum C Programming
    Replies: 4
    Last Post: 05-27-2006, 12:10 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21