Re: Questions on conversions between char* to unsigned char* and vice versa

Discussion in 'C Programming' started by Navaneeth, Jan 4, 2011.

  1. Navaneeth

    Navaneeth Guest

    On Tuesday, January 4, 2011 8:39:06 AM UTC+5:30, Ben Bacarisse wrote:
    > Navaneeth <> writes:
    >
    > > Hello Ben,

    >
    > You probably mean me. It's best, though, to address a post to the
    > group. It would be foolhardy to solicit only my opinion!
    >


    Yes, I meant you. I was replying from the groups home page where I get tree view of the messages and the reply will come as a child of your post. So I thought it'd be obvious to address to you. But I understand what you said.

    > I don't find that particularly clear. I think you are saying that you
    > intend to give your functions prototypes that use char * and to convert
    > to unsigned char * "internally":
    >
    > size_t utf8str_length(const char *s);
    >
    > and so on. If so, I agree that this is a good plan. String data should
    > usually be represented as a char array. If there are reasons to prefer
    > to manipulate it as unsigned chars then you can do that at the point
    > when that preference becomes important.
    >


    I was thinking something like,

    size_t utf8str_length(const unsigned char *s);

    and the caller should cast it. Either way, it's the same. Thanks for confirming that it is a good idea.

    thanks.
    -n
    Navaneeth, Jan 4, 2011
    #1
    1. Advertising

  2. On Jan 3, 11:58 pm, Navaneeth <> wrote:
    > On Tuesday, January 4, 2011 8:39:06 AM UTC+5:30, Ben Bacarisse wrote:
    > > Navaneeth <> writes:

    >
    > > > Hello Ben,

    >
    > > You probably mean me.  It's best, though, to address a post to the
    > > group.  It would be foolhardy to solicit only my opinion!

    >
    > Yes, I meant you. I was replying from the groups home page where I get tree view of the messages and the reply will come as a child of your post. So I thought it'd be obvious to address to you. But I understand what you said.
    >
    > > I don't find that particularly clear.  I think you are saying that you
    > > intend to give your functions prototypes that use char * and to convert
    > > to unsigned char * "internally":

    >
    > >   size_t utf8str_length(const char *s);

    >
    > > and so on.  If so, I agree that this is a good plan.  String data should
    > > usually be represented as a char array.  If there are reasons to prefer
    > > to manipulate it as unsigned chars then you can do that at the point
    > > when that preference becomes important.

    >
    > I was thinking something like,
    >
    > size_t utf8str_length(const unsigned char *s);
    >
    > and the caller should cast it. Either way, it's the same. Thanks for confirming that it is a good idea.
    >


    If we're voting, I say do it internally. Any code that always needs
    to happen before the beginning of the function might as well be
    in the function itself. Unless you expect to call the function
    more often with data that's already unsigned.

    Many design decisions for C were intended to reduce typing.
    Puting it in the function means you only have to type it once.
    This makes fewer opportunities for typos.
    luser- -droog, Jan 4, 2011
    #2
    1. Advertising

  3. Navaneeth <> writes:

    > On Tuesday, January 4, 2011 8:39:06 AM UTC+5:30, Ben Bacarisse wrote:

    <snip>
    >> I think you are saying that you
    >> intend to give your functions prototypes that use char * and to convert
    >> to unsigned char * "internally":
    >>
    >> size_t utf8str_length(const char *s);
    >>
    >> and so on. If so, I agree that this is a good plan. String data should
    >> usually be represented as a char array. If there are reasons to prefer
    >> to manipulate it as unsigned chars then you can do that at the point
    >> when that preference becomes important.
    >>

    > I was thinking something like,
    >
    > size_t utf8str_length(const unsigned char *s);
    >
    > and the caller should cast it. Either way, it's the same. Thanks for
    > confirming that it is a good idea.


    I don't think it is the same. I don't think it is a major issue but I
    would definitely make string functions have char * parameters rather
    than unsigned char *.

    --
    Ben.
    Ben Bacarisse, Jan 4, 2011
    #3
  4. Navaneeth

    Thad Smith Guest

    Re: Questions on conversions between char* to unsigned char* andvice versa

    On 1/3/2011 11:24 PM, luser- -droog wrote:
    > On Jan 3, 11:58 pm, Navaneeth<> wrote:
    >> I was thinking something like,
    >>
    >> size_t utf8str_length(const unsigned char *s);
    >>
    >> and the caller should cast it. Either way, it's the same. Thanks for confirming that it is a good idea.

    >
    > If we're voting, I say do it internally. Any code that always needs
    > to happen before the beginning of the function might as well be
    > in the function itself. Unless you expect to call the function
    > more often with data that's already unsigned.
    >
    > Many design decisions for C were intended to reduce typing.
    > Puting it in the function means you only have to type it once.
    > This makes fewer opportunities for typos.


    I agree with your conclusion, but my rationale is different. Good programming
    creates good abstractions to make human thought and planning easier. I ask,
    regardless of the implementation details, how should the caller to my module
    think about the input data, i.e., what's the straight-forward mental model? I
    am not a UTF-8 expert, but expect that many would think of the UTF-8 string as
    an implementation of Unicode using character strings. If so, char would be a
    good type. If users think of it as an array of unsigned chars, then that
    interface would be better.
    Thad Smith, Jan 5, 2011
    #4
    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. Tim  Wong
    Replies:
    5
    Views:
    21,953
    Tim Wong
    Jan 21, 2005
  2. Replies:
    3
    Views:
    8,796
  3. Navaneeth
    Replies:
    8
    Views:
    687
    Keith Thompson
    Jan 1, 2011
  4. Navaneeth
    Replies:
    3
    Views:
    327
    Seebs
    Jan 1, 2011
  5. Navaneeth
    Replies:
    1
    Views:
    317
    Ben Bacarisse
    Jan 4, 2011
Loading...

Share This Page