NO

Author Topic: need help with Unicode strings  (Read 4799 times)

jk

  • Guest
need help with Unicode strings
« on: July 11, 2005, 10:12:12 PM »
I've tried the following
Code: [Select]
#define _UNICODE
#define UNICODE

#include <stdio.h>
#include <tchar.h>

int main(int argc, char *argv[])
{
_TCHAR buf[50];

_tcscpy(buf, _T("hello"));
_tprintf(_T("%s - %d\n"), buf, sizeof(_TCHAR));
   
  return 0;
}

and I got the output
Quote
h - 2
, It should be "hello - 2". Huh? I can't see what I am doing wrong. Thanks in advance.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
need help with Unicode strings
« Reply #1 on: July 11, 2005, 10:48:42 PM »
Close - but no cigar... ;-)

You must use %ls for Unicode strings - not %s.

Pelle
/Pelle

Robert Wishlaw

  • Guest
_tmain error: Unresolved external symbol '_main'
« Reply #2 on: July 13, 2005, 12:11:52 AM »
Using _tmain instead of main, in a modified example from above, causes the error: Unresolved external symbol '_main'

Code: [Select]

#define _UNICODE
#define UNICODE

#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR *argv[])
{
   _TCHAR buf[50];

   _tcscpy(buf, _T("hello"));
   _tprintf(_T("%ls - %d\n"), buf, sizeof(_TCHAR));
   
     return 0;
}


Robert Wishlaw

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
need help with Unicode strings
« Reply #3 on: July 13, 2005, 12:27:09 PM »
Not _wmain? In tchar.h (~line 22) I define _tmain to be wmain for the Unicode case (like Micro$oft) - but I havn't implemented wmain yet (in many cases main works just as well).

I put this on the list of "things to look at in the future" a few years back, so I guess the future is here... ;-)

OK - I will look at it.

Pelle
/Pelle

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
need help with Unicode strings
« Reply #4 on: July 13, 2005, 05:10:14 PM »
Pelle,
I also miss the "_ttoi" function that should be defined in <tchar.h>.
F.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

jk

  • Guest
need help with Unicode strings
« Reply #5 on: July 13, 2005, 06:06:36 PM »
Quote from: "Pelle"
You must use %ls for Unicode strings - not %s.


That's nice, thanks. Does that mean that the code should be like this?

Code: [Select]
#ifdef _UNICODE
_tprintf(_T("%ls - %d\n"), buf, sizeof(_TCHAR));
#else
_tprintf(_T("%s - %d\n"), buf, sizeof(_TCHAR));
#endif    


Well, I'm confused. :?
Thanks again.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
need help with Unicode strings
« Reply #6 on: July 13, 2005, 07:02:42 PM »
Quote from: "jk"
Does that mean that the code should be like this?

Code: [Select]
#ifdef _UNICODE
_tprintf(_T("%ls - %d\n"), buf, sizeof(_TCHAR));
#else
_tprintf(_T("%s - %d\n"), buf, sizeof(_TCHAR));
#endif    


Yes, since _tprintf is just a macro from tchar.h that expands to wprintf (_UNICODE defined) or printf (_UNICODE not defined) - you need something like this. The C runtime functions needs a format specifier (%s or %ls) that matches the string type.

_UNICODE is for C runtime Unicode mappings in tchar.h - you also have Windows Unicode mappings through UNICODE (without the initial underscore). If you don't need anything fancy, and it's OK to use the Windows API, you might want to look at wsprintf in this case (here %s will mean different things if UNICODE is defined or not).

Quote from: "jk"

Well, I'm confused. :?
Thanks again.

Well - it *is* confusing... just look at my explanation above...!  :shock:

Pelle
/Pelle

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
need help with Unicode strings
« Reply #7 on: July 13, 2005, 07:09:34 PM »
Quote from: "frankie"
Pelle,
I also miss the "_ttoi" function that should be defined in <tchar.h>.
F.

OK - I look at this too.

Pelle
/Pelle

jk

  • Guest
need help with Unicode strings
« Reply #8 on: July 13, 2005, 07:22:50 PM »
Cool. :)

Thank you.