[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

How to determine Multibyte string length.

Owner

4/9/2011 10:44:00 PM

How to determine Multibyte string length?

So that I can allocate memory size equal to mulibyte string length.

I found there is mblen but it's only work for single character.

any help wouldbe appreciated
4 Answers

Barry Schwarz

4/10/2011 4:20:00 PM

0

On Sat, 09 Apr 2011 18:44:20 -0400, Owner <Owner@Owner-PC.com> wrote:

>How to determine Multibyte string length?
>
>So that I can allocate memory size equal to mulibyte string length.
>
>I found there is mblen but it's only work for single character.
>
>any help wouldbe appreciated

Do the functions is section 7.24 of n1256, particularly wcslen, help?

--
Remove del for email

Ben Bacarisse

4/10/2011 9:08:00 PM

0

Owner <Owner@Owner-PC.com> writes:

> How to determine Multibyte string length?
>
> So that I can allocate memory size equal to mulibyte string length.
>
> I found there is mblen but it's only work for single character.
>
> any help wouldbe appreciated

The question is a little under specified -- in particular about what to
do when the string is malformed. Assuming that we can do anything at all
in this case one answer wold be to use the mbsrtowcs function (that's
"multi-byte string, restartable, to wide character string").

Apart from having rather too many parameters, the second must be a
pointer to a pointer so it really helps to wrap this function like this:

size_t mbslen(const char *s)
{
return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
}

No doubt you have worked out that one could also just iterate calls to
mblen, but I am not sure there is anything to be gained from that
method.

--
Ben.

Bartc

4/10/2011 9:40:00 PM

0



"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.efedbdc5c606470fbaaf.20110410220743BST.87r599olwg.fsf@bsb.me.uk...
> Owner <Owner@Owner-PC.com> writes:
>
>> How to determine Multibyte string length?
>>
>> So that I can allocate memory size equal to mulibyte string length.

> Apart from having rather too many parameters, the second must be a
> pointer to a pointer so it really helps to wrap this function like this:
>
> size_t mbslen(const char *s)
> {
> return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
> }
>
> No doubt you have worked out that one could also just iterate calls to
> mblen, but I am not sure there is anything to be gained from that
> method.

Would strlen()+1 work?

--
Bartc

Ben Bacarisse

4/11/2011 12:13:00 AM

0

"BartC" <bc@freeuk.com> writes:

> "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
> news:0.efedbdc5c606470fbaaf.20110410220743BST.87r599olwg.fsf@bsb.me.uk...
>> Owner <Owner@Owner-PC.com> writes:
>>
>>> How to determine Multibyte string length?
>>>
>>> So that I can allocate memory size equal to mulibyte string length.
>
>> Apart from having rather too many parameters, the second must be a
>> pointer to a pointer so it really helps to wrap this function like this:
>>
>> size_t mbslen(const char *s)
>> {
>> return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
>> }
>>
>> No doubt you have worked out that one could also just iterate calls to
>> mblen, but I am not sure there is anything to be gained from that
>> method.
>
> Would strlen()+1 work?

Yes, that might be what the OP needs which is why I said the question
was rather underspecified. The literal interpretation of the question
would have strlen(s) + 1 as the answer, but since that is a
comparatively obvious answer (known to anyone copying even plain
strings) I decided the OP might have been asking for the number of
characters represented.

In effect I assumed the clarification meant "so that I can allocate
memory size [for wide characters] equal to mulibyte string length". I
should have made that clear.

--
Ben.