473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

multibyte,wchar_t and mblen(),wcslen()

Hi,

i have the question on how to determine the
string length of a wide string and a multibyte string:

1. Number of letters (one letter may use three bytes)
2. Number of bytes

In the code snippet *p points to one chinese word which
i copy/pasted from my browser from some chinese homepage,
followed by a german umlaut.

So i would expect '2 letters' but '5 bytes'

In the code comments the results are given.

So how can i determine the number of bytes versus the number
of letters?

Thanks
Marcel

/*
gcc -Wall -pedantic -o Hello Hello.c -std=c99
*/
#include <stdio.h/* printf() */
#include <wchar.h/* wprintf (use -std=c99 compile flag */
#include <locale.h/* setlocal() */
#include <string.h/* strlen() */
#include <stdlib.h/* mbstowcs() mblen() */

int main()
{
setlocale(LC_CTYPE, "en_US");
char *p = "統ö";

size_t len = mblen(p, 100);
printf("multibyte string: '%s' strlen=%d mblen=%d\n", p, (int)strlen(p), (int)len);
/* --multibyte string: '統ö' strlen=5 mblen=1 */

wchar_t pwcs[126];
mbstowcs(pwcs, p, 124);
// wcslen() computes the number of wide characters in the string??
printf("wide string: '%ls' wcslen=%d\n", pwcs, (int)wcslen(pwcs));
/* --wide string: '統ö' wcslen=5 */
return 0;
}
Nov 23 '06 #1
1 5990
Marcel Ruff <mr@marcelruff.infowrites:
Hi,

i have the question on how to determine the
string length of a wide string and a multibyte string:

1. Number of letters (one letter may use three bytes)
2. Number of bytes

In the code snippet *p points to one chinese word which
i copy/pasted from my browser from some chinese homepage,
followed by a german umlaut.

So i would expect '2 letters' but '5 bytes'

In the code comments the results are given.

So how can i determine the number of bytes versus the number
of letters?

Thanks
Marcel

/*
gcc -Wall -pedantic -o Hello Hello.c -std=c99
*/
#include <stdio.h/* printf() */
#include <wchar.h/* wprintf (use -std=c99 compile flag */
#include <locale.h/* setlocal() */
#include <string.h/* strlen() */
#include <stdlib.h/* mbstowcs() mblen() */

int main()
{
setlocale(LC_CTYPE, "en_US");
You need to check the return is non-null or you may not be setting
anything here. Unless you are sure that the default encoding will be
multi-byte one, I would specify it. In other words I would use:

if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL) {
...
}

Also, if setlocale succeeds, you might like to print the returned
string. The standard does not say much about what the string will
mean but in practise *you* will probably know what it means!
char *p = "統ö";
It is unwise to use anything but 7-bit characters when posting
code. You should use something like:

char *p = "\xC3\xB6"; // the UTF-8 encoding of o with diaeresis

I am not at all sure what bytes are in your string, so I can not be
sure about what follows.
>
size_t len = mblen(p, 100);
printf("multibyte string: '%s' strlen=%d mblen=%d\n", p, (int)strlen(p), (int)len);
/* --multibyte string: '統ö' strlen=5 mblen=1 */
That looks to me like the default encoding for en_US on your system
accepts the first byte of that string as character on its own (i.e. it
is a plain 8-bit character set encoding). Post code and output when
you (a) specify a locale setting that includes an encoding (as above) and
(b) your example string is written out using hex in the source.
>
wchar_t pwcs[126];
mbstowcs(pwcs, p, 124);
// wcslen() computes the number of wide characters in the string??
printf("wide string: '%ls' wcslen=%d\n", pwcs, (int)wcslen(pwcs));
/* --wide string: '統ö' wcslen=5 */
return 0;
}
--
Ben.
Nov 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Zygmunt Krynicki | last post by:
Hello I've browsed the FAQ but apparently it lacks any questions concenring wide character strings. I'd like to calculate the length of a multibyte string without converting the whole string. ...
3
by: Simon Morgan | last post by:
Hi, The following code is meant to validate a string of multibyte characters by using mbcheck() to call mblen() on each character on the string passed to it. The problem is that it isn't working...
3
by: Jordan Abel | last post by:
Is there a function to find the length, in wide characters, of a multibyte string?
0
by: Munch | last post by:
my C program deals with single byte characters but now i want to fetch multibyte data stored in the datbase so what all changes i need to make to the code so that it handles multibyte data as well....
0
by: Munch | last post by:
my C program deals with single byte characters but now i want to fetch multibyte data stored in the datbase so what all changes i need to make to the code so that it handles multibyte data as well. ...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
13
by: TK | last post by:
Hi, how can I handle multibyte characters like ä, ü (german vowel mutation)? This does't work: switch(c) case 'ä': ... some action
16
by: Michael Brennan | last post by:
I guess this question only applies to programming applications for UNIX, Windows and similiar. If one develops something for an embedded system I can understand that wchar_t would be unnecessary. ...
1
johny10151981
by: johny10151981 | last post by:
Hello Everybody, I am having a problem with converting value from multibyte to widechar input variable is data output variable is data_m #define CP_SHIFT_JIS 932
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.