Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to determine Multibyte string length.

Reply
Thread Tools

How to determine Multibyte string length.

 
 
Owner
Guest
Posts: n/a
 
      04-09-2011
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
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-10-2011
On Sat, 09 Apr 2011 18:44:20 -0400, Owner <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-10-2011
Owner <(E-Mail Removed)> 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.
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      04-10-2011


"Ben Bacarisse" <(E-Mail Removed)> wrote in message
news:0.efedbdc5c606470fbaaf.20110410220743BST.87r5 (E-Mail Removed)...
> Owner <(E-Mail Removed)> 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

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      04-11-2011
"BartC" <(E-Mail Removed)> writes:

> "Ben Bacarisse" <(E-Mail Removed)> wrote in message
> news:0.efedbdc5c606470fbaaf.20110410220743BST.87r5 (E-Mail Removed)...
>> Owner <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How should I handle the multibyte char set string in C++? Dancefire C++ 10 05-05-2007 09:49 PM
Hi,how to Convert unicode string to MultiByte in VB.net? Billow ASP .Net 2 12-01-2005 03:19 PM
Unicode/multibyte string support in Ruby1.9/Ruby summary? David Garamond Ruby 8 01-16-2005 02:11 PM
Multibyte string length Zygmunt Krynicki C Programming 19 10-14-2003 12:19 PM



Advertisments