CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

converting long to hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
MAKInnovation



Joined: 16 Nov 2010
Posts: 61

View user's profile Send private message

converting long to hex
PostPosted: Sat Jan 25, 2014 2:21 am     Reply with quote

Dear Friends;
I want to convert long int value into an Hex value.
and then i have to send two bytes on the serial port.

For example the int value is 635. then i have to convert it into an hex value i.e 27B.

Then i have to send it to the serial port on two bytes, one would be 7B and second would be 02.

Can anyone know the simple conversion code.
Thanks
Ashraf
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Sat Jan 25, 2014 2:56 am     Reply with quote

Step back.

A _long_ int, implies 16bits, so four hex digits, not two.

printf can output a long just as easily as a short integer:
Code:

   int16 val=635;

   printf("%04LX", val);

will send 027B over the serial. Four hex digits.

However you seem to be confusing hex with raw bytes.

Val in memory, comprises the two bytes '01111011', and '00000010', which can be represented as '7B', and '02' in hex. So for what you describe you only need to send the bytes. No 'conversion' required at all:
Code:

   union {
      int8 b[2];
      int16 w;
  } access;

  access.w=635;
  putc(access.b[0]);
  putc(access.b[1]);

  //Or use the CCS function 'make8'
  int16 val=635;

  putc(make8(val,0)); //send the first byte
  putc(make8(val,1)); //send the second


You are just sending raw binary, no 'hex' involved.

The advantage of the union, is that it can also be used the other way, so if you write values to access.b[0], and access.b[1], then access.w, contains the 16bit value.

Best Wishes
MAKInnovation



Joined: 16 Nov 2010
Posts: 61

View user's profile Send private message

PostPosted: Sat Jan 25, 2014 3:47 am     Reply with quote

I am now getting the correct value. Thanks for your help Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group