[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Strings in UNICODE

Posted on 2001-07-26
8
Medium Priority
?
797 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

656 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