473,765 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1794
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.invalidwrot e:
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...@gmai l.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...@gm ail.comwrote:
On Jul 26, 9:14 am, Seth <king.s...@gmai l.comwrote:Than ks 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...@gm ail.comwrote:
On Jul 26, 9:14 am, Seth <king.s...@gmai l.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************ *************** *******@w7g2000 hsa.googlegroup s.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

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

Similar topics

5
1922
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 binary by 10000110 and char 2 by 10101010 I would like my int to be represented by 1000011010101010 I know short in would probably be best but for right now int will work, I may have additions I might need to add.
58
10179
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 code... TCHAR myArray; DoStuff(myArray);
4
1706
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 bankedDataMemoryLayout(NUMBER_BANKS); but an empty vector TYPE_VECTOR_BANKED_MEMORY_DATA bankedDataMemoryLayout; The program crashes on the 3rd insertion???
3
12967
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 following:"ADDASSF\366lhandel ADFS" Could someone please tell me how to print this? Thanks.
9
6279
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 can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like : Start from the right most charecter of the string to the left of the
44
2676
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. The reason I ask is that I am sending things across an rs485 serial network, and the protocol is set up to send byte by byte. It sends one (8 bit) char at a time. I would like to send a float across this network in the form of four chars...
14
6416
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 I go about doing it ? Thanks
9
4099
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 puts() to print to screen ????
0
1292
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
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9951
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.