Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Simple Work with UNICODE

newtronnewtron Member Posts: 4
Hello everybody,
can anyone help me? :S

how can I get only one character from
WCHAR *str = L"string";
for example only "r", when I'm typing this
MessageBox(NULL,&str[3],NULL,NULL);
it gets "ring" and not "r"

please help me ...

Comments

  • untiountio Member Posts: 14
    Hi,
    If you only need a character in unicode:
    WCHAR *mystr = L"string";
    WCHAR uchar;
    uchar = mystr[3];
    &mystr[3] is the address of fourth element. mystr is the address of the first element. &mystr[3] is the address of the string "ing". mystr[3] is only one character. If you need a character do not pass an address.
    Look at this:
    TCHAR mystr[500];
    _tcscpy(mystr, TEXT("hello"));
    TCHAR mychar;
    mychar = mystr[1];
    mystr[2] = TEXT('');

    MessageBox(NULL, mystr, TEXT("Attention"), MB_OK);

    TEXT is a macro that transforms the character or string to unicode if UNICODE and _UNICODE are defined or to ansi if not. TCHAR is transformed to WCHAR or CHAR in the same condition. And if you look at the file tchar.h you will find the macro _tcscpy among others. It is transformed to strcpy if UNICODE and _UNICODE are not defined and to the unicode version if they are defined. And if you define UNICODE and _UNICODE, MessageBox is transformed to MessageBoxW and, if not define them, is transformed to MessageBoxA. Working with wide characters or with ansi characters.
    Imagine. It is possible to compile your program in unicode or ansi simply changing the two definitions.
    Good, I think that in Visual C, it suffice with define only UNICODE, but in mingw you must define the two words:
    #define UNICODE
    #define _UNICODE
    #include
    #include

    #include
    ...

    I hope that this could be useful.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    : Hello everybody,
    : can anyone help me? :S
    :
    : how can I get only one character from
    : WCHAR *str = L"string";
    : for example only "r", when I'm typing this
    : MessageBox(NULL,&str[3],NULL,NULL);
    : it gets "ring" and not "r"
    :
    : please help me ...
    :

    [color=Blue]Since message box function takes a string as parameter - you need to CREATE a second string with only one character - one you need. Also, character indexes begin with 0, so 'r' will be addressed as str[2] and not str[3].[/color]

    [code]
    WCHAR *str = L"string";
    WCHAR buf [2]; [color=Green]// New string with room for ONLY one character[/color]

    ...

    buf [0] = str [2]; [color=Green]// Move 'r' as first character of new string[/color]
    buf [1] = 0; [color=Green]// Terminate new string with null as C lang. needs[/color]
    MessageBox (0, buf, 0, 0); [color=Green]// Now you will see only one character[/color]
    [/code]
  • untiountio Member Posts: 14
    Hi,
    At first sight, I did not understand the question. Why a string with a single character?. Right. For every ansi character function there is its counterpart for wide characters and its corresponding macro in tchar.h.
    You can use (with tchar.h and wchar.h):
    TCHAR string[100];
    _stprintf(string, TEXT("%d %c %u %f"), decimalv, tcharacterv, unsignedv, floatv).
    And only with wchar.h:
    WCHAR string[100];
    swprintf(string, L"%d %c %u %f", decimalv, wcharacterv, unsignedv, floatv)
    _stprintf, I think that for wide characters it is translated to swprintf and to sprintf for ansi characters depending if UNICODE is defined.
    I personally use tchar.h within all my projects. Like this I can switch from unicode to ansi with one or two defines.

    May be is interesting to you:
    WCHAR wstr1[] = L"a string";
    WCHAR wstr2[2];
    swprintf(wstr2, L"%c", wstr1[2]);

    The solution from AsmGuru62 is perfect, because you need only one character. But with more than one character may be a little confusing.

    Sorry for my wrong response.
  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]Good post, I just want to notice that ANSI text will slow down your program on any Windows version starting from Win NT (ANSI is OK for Win95,98 - but these are last versions). This happens, because core of today's Windows is working with UNICODE only and if program uses ANSI, then EACH STRING TAKING FUNCTION (there are a lot of these in Win32 API!!) must convert the ANSI to UNICODE before passing it to Windows core and that slows the code down.

    I write all my code for UNICODE only.[/color]
Sign In or Register to comment.