473,322 Members | 1,405 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,322 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 2328
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.