Retrieving two chars from unsigned char * | | |
I have a unsigned char *data that points to a 32 character string. I
need to retrieve 2 characters in the middle of the string.
What is the best way to do this?
Platform is embedded 8-bit atmel processor.
Thanks | | | | re: Retrieving two chars from unsigned char *
Seth wrote: Quote:
I have a unsigned char *data that points to a 32 character string. I
need to retrieve 2 characters in the middle of the string.
>
What is the best way to do this?
Platform is embedded 8-bit atmel processor.
unsigned char *data = ...whatever...;
unsigned char c1 = data[15];
unsigned char c2 = data[16];
This should work on all processors: Atmel, Hormel,
and Wilhelmtell.
--
Eric Sosman esosman@ieee-dot-org.invalid | | | | re: Retrieving two chars from unsigned char *
Thanks a lot.
On a related note when I am comparing two unsigned char with strcmp I
get:
warning: pointer targets in passing argument 2 of 'strcmp' differ in
signedness
Should I be worried about this?
Thanks
On Jul 25, 11:25*pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote: Quote:
Seth wrote: Quote:
I have a unsigned char *data that points to a 32 character string. I
need to retrieve 2 characters in the middle of the string.
> Quote:
What is the best way to do this?
Platform is embedded 8-bit atmel processor.
>
* * * * unsigned char *data = ...whatever...;
* * * * unsigned char c1 = data[15];
* * * * unsigned char c2 = data[16];
>
* * *This should work on all processors: Atmel, Hormel,
and Wilhelmtell.
>
--
Eric Sosman
esos...@ieee-dot-org.invalid
| | | | re: Retrieving two chars from unsigned char *
Seth wrote: Quote:
Thanks a lot.
On a related note when I am comparing two unsigned char with strcmp I
get:
warning: pointer targets in passing argument 2 of 'strcmp' differ in
signedness
>
Should I be worried about this?
>
Thanks
If you array is composed solely of the characters specified in the basic
character set, then you needn't be worried. If your array may consist
of any byte value then interpreting an unsigned value as a signed value
may cause overflow and undefined behaviour. | | | | re: Retrieving two chars from unsigned char *
On Jul 26, 9:14*am, Seth <king.s...@gmail.comwrote: Quote:
Thanks a lot.
On a related note when I am comparing two unsigned char
I take it that you meant to say "comparing two unsigned char *" Quote:
Should I be worried about this?
Depends on you charset. If you are using ASCII, no reasons to worry
about that. | | | | re: Retrieving two chars from unsigned char *
On Fri, 25 Jul 2008 21:14:12 -0700, Seth wrote: Quote:
On a related note when I am comparing two unsigned char with strcmp I
get:
warning: pointer targets in passing argument 2 of 'strcmp' differ in
signedness
>
Should I be worried about this?
You need to cast them to char * and not rely on an implicit conversion
your compiler happens to support. Other than that, no, you don't need to
worry about it. strcmp works with unsigned char * anyway: it converts its
parameters from char * back to unsigned char * before reading the strings. | | | | re: Retrieving two chars from unsigned char *
On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote: Quote:
On Jul 26, 9:14 am, Seth <king.s...@gmail.comwrote:Thanks a lot. Quote:
Should I be worried about this?
>
Depends on you charset. If you are using ASCII, no reasons to worry
about that.
Please explain why ASCII matters (hint: it doesn't) | | | | re: Retrieving two chars from unsigned char *
On Jul 26, 10:39 am, vipps...@gmail.com wrote: Quote:
On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote:
> Quote:
On Jul 26, 9:14 am, Seth <king.s...@gmail.comwrote:> Quote:
Thanks a lot.
Should I be worried about this?
> Quote:
Depends on you charset. If you are using ASCII, no reasons to worry
about that.
>
Please explain why ASCII matters (hint: it doesn't)
I think I snipped context here.
Anyway the discussion was about pointer signedness passed to strcmp().
OP asked if it did matter and rahul suggested that if ASCII is
available it doesn't matter.
This code is perfectly valid,
....
unsigned char p[] = "hello", s[] = "world";
if(strcmp(p, s)) printf("They match!\n");
....
As is
if(strcmp((void *)p, (void *)s)) printf("They match!\n");
Though the latter will make (most) compilers not warn. | | | | re: Retrieving two chars from unsigned char *
In article <7174264f-12f7-4b7e-9b5e-7e3da17fd134@w7g2000hsa.googlegroups.com>,
<vippstar@gmail.comwrote: Quote:
>Anyway the discussion was about pointer signedness passed to strcmp().
>OP asked if it did matter and rahul suggested that if ASCII is
>available it doesn't matter.
>This code is perfectly valid,
>
>...
>unsigned char p[] = "hello", s[] = "world";
>
>if(strcmp(p, s)) printf("They match!\n");
>...
strcmp returns 0 if the strings match:
if (0 == strcmp(p, s)) printf("They match!\n"); | | | | re: Retrieving two chars from unsigned char *
On Jul 26, 11:05 am, i...@localhost.claranet.nl (Ike Naar) wrote: Quote:
<vipps...@gmail.comwrote:
Quote: Quote:
if(strcmp(p, s)) printf("They match!\n");
...
>
strcmp returns 0 if the strings match:
>
if (0 == strcmp(p, s)) printf("They match!\n");
Heh. I wanted printf to print "They don't match!\n" though halfway I
forgot about it :P | | | | re: Retrieving two chars from unsigned char *
On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vippstar@gmail.com wrote: Quote:
>On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote: Quote:
>On Jul 26, 9:14 am, Seth <king.s...@gmail.comwrote:Thanks a lot. Quote:
Should I be worried about this?
>>
>Depends on you charset. If you are using ASCII, no reasons to worry
>about that.
>
>Please explain why ASCII matters (hint: it doesn't)
strcmp is expecting to process "plain" char. On a system where char
is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
in the unique range of unsigned char, attempting to treat the value as
signed could produce incorrect results:
Attempting to store too large a value in an object yields
undefined behavior (C90) or "strange" behavior (C99)
Comparing two such object which are unequal may produce the
"wrong" less than/greater than result.
ASCII "matters" only because its seven bit values always fit in a
eight bit (minimum) char.
Remove del for email | | | | re: Retrieving two chars from unsigned char *
Barry Schwarz wrote: Quote:
On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vippstar@gmail.com wrote:
> Quote:
>On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote: Quote:
>>On Jul 26, 9:14 am, Seth <king.s...@gmail.comwrote:Thanks a lot.
>>>Should I be worried about this?
>>Depends on you charset. If you are using ASCII, no reasons to worry
>>about that.
>Please explain why ASCII matters (hint: it doesn't)
>
strcmp is expecting to process "plain" char. On a system where char
is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
in the unique range of unsigned char, attempting to treat the value as
signed could produce incorrect results:
However, strcmp won't treat the values as signed char.
N869
7.21.4 Comparison functions
[#1] The sign of a nonzero value returned by the comparison
functions memcmp, strcmp, and strncmp is determined by the
sign of the difference between the values of the first pair
of characters (both interpreted as unsigned char) that
differ in the objects being compared.
--
pete | | | | re: Retrieving two chars from unsigned char *
On Sat, 26 Jul 2008 09:01:22 -0700, Barry Schwarz <schwarzb@dqel.com>
wrote: Quote:
>strcmp is expecting to process "plain" char. On a system where char
>is signed, CHAR_MAX may be (probably is) < UCHAR_MAX. For any values
>in the unique range of unsigned char, attempting to treat the value as
>signed could produce incorrect results:
>
Attempting to store too large a value in an object yields
>undefined behavior (C90) or "strange" behavior (C99)
>
Comparing two such object which are unequal may produce the
>"wrong" less than/greater than result.
>
>ASCII "matters" only because its seven bit values always fit in a
>eight bit (minimum) char.
Please disregard this obviously incorrect answer which proceeds from a
false assumption.
I have to remember to read the higher level paragraphs when reading
the function descriptions.
Remove del for email |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,533 network members.
|