J
jonathanztaub
I'm not a C programmer but have to fix some legacy application written
in C (deployed on Linux).
It seems that the "strlen" function returns the number of bytes for a
character array.
I need to get the number of characters. For example, some Japanese
characters are 3 bytes per character. If I had a character array
containing two of these characters, I would like to get 2 (length) as
opposed to 6 (byte size). I cannot make extensive code modifications
to this application and would like to make all necessary changes
within the function scope:
static int verifyChar(char *dst, char *src, int n)
{
/* check length */
if (strlen(src) > 100)
return -1;
return fieldCopy(dst, src, n);
}
I tried playing around with wcslen (wchar.h) and other conversion
methods I found on the internet like the following:
wchar_t wcs[10];
char mbs[10]="\u00A9"; /* copyright character, though it is a
single byte */
char *ptr = mbs; /* pointer to the mbs string */
int length;
/* Determine the length of the multibyte string pointed to by
*/
/* mbs. Store the multibyte characters in the wchar_t array
*/
/* pointed to by wcs.
*/
length = mbsrtowcs(wcs, (const char**)&ptr, SIZE, NULL);
Length returns the value of -1. However, if I changed the mbs value to
"abcd", it'll return four.
Please keep in mind that I'm not C savvy.
Thanks.
in C (deployed on Linux).
It seems that the "strlen" function returns the number of bytes for a
character array.
I need to get the number of characters. For example, some Japanese
characters are 3 bytes per character. If I had a character array
containing two of these characters, I would like to get 2 (length) as
opposed to 6 (byte size). I cannot make extensive code modifications
to this application and would like to make all necessary changes
within the function scope:
static int verifyChar(char *dst, char *src, int n)
{
/* check length */
if (strlen(src) > 100)
return -1;
return fieldCopy(dst, src, n);
}
I tried playing around with wcslen (wchar.h) and other conversion
methods I found on the internet like the following:
wchar_t wcs[10];
char mbs[10]="\u00A9"; /* copyright character, though it is a
single byte */
char *ptr = mbs; /* pointer to the mbs string */
int length;
/* Determine the length of the multibyte string pointed to by
*/
/* mbs. Store the multibyte characters in the wchar_t array
*/
/* pointed to by wcs.
*/
length = mbsrtowcs(wcs, (const char**)&ptr, SIZE, NULL);
Length returns the value of -1. However, if I changed the mbs value to
"abcd", it'll return four.
Please keep in mind that I'm not C savvy.
Thanks.