Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Questions on conversions between char* to unsigned char* and vice versa

Reply
Thread Tools

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

 
 
Navaneeth
Guest
Posts: n/a
 
      01-04-2011
On Tuesday, January 4, 2011 8:39:06 AM UTC+5:30, Ben Bacarisse wrote:
> Navaneeth <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
luser- -droog
Guest
Posts: n/a
 
      01-04-2011
On Jan 3, 11:58*pm, Navaneeth <(E-Mail Removed)> wrote:
> On Tuesday, January 4, 2011 8:39:06 AM UTC+5:30, Ben Bacarisse wrote:
> > Navaneeth <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-04-2011
Navaneeth <(E-Mail Removed)> 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.
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      01-05-2011
On 1/3/2011 11:24 PM, luser- -droog wrote:
> On Jan 3, 11:58 pm, Navaneeth<(E-Mail Removed)> 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.
 
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: Questions on conversions between char* to unsigned char* and vice versa Navaneeth C Programming 1 01-04-2011 03:09 AM
Questions on conversions between char* to unsigned char* and vice versa Navaneeth C Programming 8 01-01-2011 10:39 PM
Re: Questions on conversions between char* to unsigned char* and vice versa Navaneeth C Programming 3 01-01-2011 01:15 AM
converting int and short to byte array and vice versa carmen Java 4 01-12-2010 05:00 PM
Convert std::string to std::vector<unsigned char> and vice versa timor.super@gmail.com C++ 3 04-02-2007 02:12 PM



Advertisments