473,289 Members | 1,829 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,289 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 5966
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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.