473,396 Members | 1,877 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,396 software developers and data experts.

in Embedded C, how to convert a Returned Hex value in the form of “Unsigned Char”

We are writing a code for I2C interface,
where we are reading a 16 bit Hex number as two 8 bit Hex MSB and LSB, and we are returning these values as "Unsigned Char".

we want to concatenate these MSB and LSB "char" values, and finally we need one "Integer" value for our further processing.

for example: the following 2 methods are returning one "Unsigned Char" value, each
Expand|Select|Wrap|Line Numbers
  1.  
  2.    unsigned char i2c_readAck(void)
  3. {
  4.     TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  5.     while(!(TWCR & (1<<TWINT)));
  6.  
  7.     return TWDR;
  8.  
  9. }/* i2c_readAck */
  10.  
  11.  
  12.  
  13.    unsigned char i2c_readNak(void)
  14. {
  15.     TWCR = (1<<TWINT) | (1<<TWEN);
  16.     while(!(TWCR & (1<<TWINT)));
  17.  
  18.     return TWDR;
  19.  
  20. }/* i2c_readNak */
  21.  

we have to fetch MSB and LSB values from these 2 methods who are actual HEX values needed,
but in unsigned char type, concatenate it, and the finally the concatenated value must be converted to usable Integer format,

we are finding the the conversation part very tricky,
can anyone help us??
Mar 31 '11 #1
1 2332
donbock
2,426 Expert 2GB
I trust TWCR and TWDR are defined as something like this:
Expand|Select|Wrap|Line Numbers
  1. volatile unsigned char *cr;
  2. volatile unsigned char *dr;
  3. #define TWCR *cr
  4. #define TWDR *dr
If the msb and lsb values are moved into wider unsigned variables you can then combine the values stored in the wider variables.
Mar 31 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: b83503104 | last post by:
When are they not consistent?
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
1
by: Sergey Muschin | last post by:
Hi there, i need to send a socket message to TCP socket server. Server has been implemented with VC 6.0 and expects from client the message in this format typedef struct { int msg_nbr; ...
2
yabansu
by: yabansu | last post by:
Hi, I just want to know if there is a way of converting a string variable into a const unsigned char pointer. Alternatively, I expect a blabla() function as the following: string str = "Hello";...
0
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error:...
7
by: richardpeter | last post by:
I am attempting to convert a string to unsigned char. The Method I use appears to work but the function call fails. I am using MSVC++ Express 2.0.50727 on Win XP with an AMD Processor. Forgive me...
0
by: Simon Posnjak | last post by:
Hi! I have a C module for which I created a wrapper with swig. The function def is: C: int some_thing(unsigned char * the_str); eg:
1
by: Simon Posnjak | last post by:
On Mon, May 5, 2008 at 4:16 PM, Jean-Paul Calderone <exarkun@divmod.comwrote: some_module.some_thing(the_string) function is a swig generated function from a C lib. The C lib function expects...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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...
0
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,...

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.