Solved

Strings in UNICODE

Posted on 2001-07-26
8
757 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now