How to determine Multibyte string length.

Discussion in 'C Programming' started by Owner, Apr 9, 2011.

  1. Owner

    Owner Guest

    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
     
    Owner, Apr 9, 2011
    #1
    1. Advertising

  2. On Sat, 09 Apr 2011 18:44:20 -0400, Owner <> 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
     
    Barry Schwarz, Apr 10, 2011
    #2
    1. Advertising

  3. Owner <> 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.
     
    Ben Bacarisse, Apr 10, 2011
    #3
  4. Owner

    BartC Guest

    "Ben Bacarisse" <> wrote in message
    news:...
    > Owner <> 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
     
    BartC, Apr 10, 2011
    #4
  5. "BartC" <> writes:

    > "Ben Bacarisse" <> wrote in message
    > news:...
    >> Owner <> 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.
     
    Ben Bacarisse, Apr 11, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam
    Replies:
    3
    Views:
    14,109
    Karl Seguin
    Feb 17, 2005
  2. Zygmunt Krynicki

    Multibyte string length

    Zygmunt Krynicki, Oct 9, 2003, in forum: C Programming
    Replies:
    19
    Views:
    709
    Dan Pop
    Oct 14, 2003
  3. Replies:
    5
    Views:
    667
    John W. Kennedy
    Jan 11, 2007
  4. Jordan Abel

    multibyte length

    Jordan Abel, Mar 3, 2006, in forum: C Programming
    Replies:
    3
    Views:
    318
    Micah Cowan
    Mar 3, 2006
  5. kobayashi
    Replies:
    9
    Views:
    212
    Evan Driscoll
    Nov 26, 2012
Loading...

Share This Page