Ttelmah
Joined: 11 Mar 2010 Posts: 19512
|
|
Posted: Sat Jan 25, 2014 2:56 am |
|
|
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 |
|