473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 26 '08 #1
12 1768
Seth wrote:
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
es*****@ieee-dot-org.invalid
Jul 26 '08 #2
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:
Seth wrote:
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
esos...@ieee-dot-org.invalid
Jul 26 '08 #3
Seth wrote:
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.

Jul 26 '08 #4
On Jul 26, 9:14*am, Seth <king.s...@gmail.comwrote:
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 *"
Should I be worried about this?
Depends on you charset. If you are using ASCII, no reasons to worry
about that.
Jul 26 '08 #5
On Fri, 25 Jul 2008 21:14:12 -0700, Seth wrote:
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.
Jul 26 '08 #6
On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote:
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)
Jul 26 '08 #7
On Jul 26, 10:39 am, vipps...@gmail.com wrote:
On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote:
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)
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.
Jul 26 '08 #8
In article <71**********************************@w7g2000hsa.g ooglegroups.com>,
<vi******@gmail.comwrote:
>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");

Jul 26 '08 #9
On Jul 26, 11:05 am, i...@localhost.claranet.nl (Ike Naar) wrote:
<vipps...@gmail.comwrote:
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
Jul 26 '08 #10
On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vi******@gmail.com wrote:
>On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote:
>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:

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
Jul 26 '08 #11
Barry Schwarz wrote:
On Sat, 26 Jul 2008 00:39:39 -0700 (PDT), vi******@gmail.com wrote:
>On Jul 26, 10:26 am, rahul <rahulsin...@gmail.comwrote:
>>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
Jul 26 '08 #12
On Sat, 26 Jul 2008 09:01:22 -0700, Barry Schwarz <sc******@dqel.com>
wrote:
>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
Jul 26 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bob Smith | last post by:
Hello all, I have a question and I am not sure how I should go about doing it. I have two unsigned chars and one int. I want to copy both chars to the int. for example if char a was represented...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: codewarr2000 | last post by:
Having problem with retrieving a class instance item from a Vector. This is the result of the code below. Also a weird note: If I dont declare as: TYPE_VECTOR_BANKED_MEMORY_DATA...
3
by: RHNewBie | last post by:
Hello, I have the following string "ADDASSFölhandel ADFS" which is referred to by a variable whose type declaration is "usigned char *extendedasciistring". When I print using %s I see the...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
44
by: Kosio | last post by:
-Hello, I'm looking for a way to deconstruct a (32 bit) float value to four (8 bit) char values. I then need a way to take those four (8 bit) char values and construct a (32 bit) float value. ...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
0
by: ikkenmt | last post by:
hi friends... i need to do this as simple as can be... to swap the bits of 3 chars...24 bits i must consider that the bits are in order like CBA 10101010 10101010 10101010
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.