Solved

Strings in UNICODE

Posted on 2001-07-26
8
765 Views
Last Modified: 2007-11-27
Hi !

Bellow is some simple test program:

#include "tchar.h"

#define UNICODE
#define _UNICODE

int main(int argc, char* argv[])
{

     TCHAR MyString[80];
     _stprintf(MyString,_T("URL is i %d"),6);

     //wprintf(MyString,"%s");
        printf(MyString,"LLL");
     return 0;
}

As far as I undertstand, when UNICODE is defined, TCHAR should be translated as UNICODE string, and if not - as ANSI string. However, it seems that this code allways comilled in ANSI, because I when I use wprintf function, even when UNICODE is defined, I allways get "C2664 error" - cannot convert parameter 1 from 'char [80]' to 'const unsigned short *'. At the same time, "printf' function allways get compilled Ok.

What is going on there ?
What is the right way to make UNICODE string operations ?

Thanks a lot !
0
Comment
Question by:ab11
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6321454
Those definitions will affect code found in windows.h (and files it includes).   But you don't appear to be including windows.h.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6321460
Apparently it also affects procedures and code defined in the C++ run-time library (RTL) too.   Howeve,r you need to define it BEFORE including those files.  i.e. those files contain conditional compilation (#ifdef....#endif) that uses this define, so definiing this value after them is too late.

You need to move the #defines to the top of the program, or define them using a command-line parameter for the compiler.
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6321489
out the 2 UNICODE and _UNICODE defines in project\settings\C/C++\PreprocessorDefinitions

this applies if you have VC++ :)

your code will work just fine and you won't have to take care of putting the defines in some specific place in your code.

if you use something else, be carefull to define the 2 before ANY includes of h files :)
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 6321658
>>even when UNICODE is defined, I allways get "C2664
>>error" - cannot convert parameter
>>1 from 'char [80]' to 'const unsigned short *'

This is not related toyour string variable, but to the 'wprintf()' format string:

wprintf(MyString,"%s"); // "%s" represents an *ANSI* char* !!!

should read

wprintf(MyString,L"%s"); // L"%s" represents an *UNICODE* wchar_t* !!!


which should logically be

wprintf(L"%s", MyString);


It'd be better to use '_tprintf()', though:


_tprintf(_T("%s"), MyString);

to keep it compatible when you don't '#define UNICODE'


Example

/* PRINTF.C: This program uses the printf and wprintf functions
 * to produce formatted output.
 */

#include <stdio.h>

void main( void )
{
   char   ch = 'h', *string = "computer";
   int    count = -9234;
   double fp = 251.7366;
   wchar_t wch = L'w', *wstring = L"Unicode";

   /* Display integers. */
   printf( "Integer formats:\n"
           "\tDecimal: %d  Justified: %.6d  Unsigned: %u\n",
           count, count, count, count );

   printf( "Decimal %d as:\n\tHex: %Xh  C hex: 0x%x  Octal: %o\n",
            count, count, count, count );

   /* Display in different radixes. */
   printf( "Digits 10 equal:\n\tHex: %i  Octal: %i  Decimal: %i\n",
            0x10, 010, 10 );

   /* Display characters. */

   printf("Characters in field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch);
   wprintf(L"Characters in field (2):\n%10C%5hc%5c%5lc\n", ch, ch, wch, wch);

   /* Display strings. */

   printf("Strings in field (1):\n%25s\n%25.4hs\n\t%S%25.3ls\n",
   string, string, wstring, wstring);
   wprintf(L"Strings in field (2):\n%25S\n%25.4hs\n\t%s%25.3ls\n",
       string, string, wstring, wstring);

   /* Display real numbers. */
   printf( "Real numbers:\n\t%f %.2f %e %E\n", fp, fp, fp, fp );

   /* Display pointer. */
   printf( "\nAddress as:\t%p\n", &count);

   /* Count characters printed. */
   printf( "\nDisplay to here:\n" );
   printf( "1234567890123456%n78901234567890\n", &count );
   printf( "\tNumber displayed: %d\n\n", count );

0
 

Author Comment

by:ab11
ID: 6321808
Ok, thanks for all
0
 

Author Comment

by:ab11
ID: 6321815
Thanks for all your help !
In fact, it's really not much practical to define UNICODE after the include files :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6321856
>>In fact, it's really not much practical to define
>>UNICODE after the include files :)

Yes, but

>>wprintf(MyString,"%s");

Is

- logically wrong
- and produces 'cannot convert..' regardless where you #define UNICODE
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6322010
jkr is totally right
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question