View previous topic :: View next topic |
Author |
Message |
delilcodes
Joined: 12 May 2021 Posts: 5
|
Dec to Hex - Merge Digits to a Num |
Posted: Sat May 15, 2021 12:16 pm |
|
|
I am trying to change dec numbers to hex. It turned out to be harder than I thought. Maybe I think too much. I just want to turn change just non-fractional side of number.
Code: |
doubleWeigth = 695.581;
intPart = (doubleWeigth*100);
hexDigits[0] = intPart%16;
intPart = (intPart/16-(intPart/16)%1);
hexDigits[1] = intPart%16;
intPart = (intPart/16-(intPart/16)%1);
hexDigits[2] = intPart%16;
for(i=0;i<3;i++){
switch (hexDigits[i]){
case 10:
hexDigits[i] = 0x10;
break;
case 11:
hexDigits[i] = 0x11;
break;
case 12:
hexDigits[i] = 0x12;
break;
case 13:
hexDigits[i] = 0x13;
break;
case 14:
hexDigits[i] = 0x14;
break;
case 15:
hexDigits[i] = 0x15;
break;
}
}
|
I stacked here. How can i merge the numbers as one hex number? |
|
|
delilcodes
Joined: 12 May 2021 Posts: 5
|
|
Posted: Sat May 15, 2021 1:00 pm |
|
|
I used the expression like;
Code: | i = (hexDigits[2]<<16)|(hexDigits[1]<<8)|(hexDigits[0]);
|
But i didnot work. Whats the problem here? |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Sat May 15, 2021 2:28 pm |
|
|
Are you trying to store each hex digit as an element of an array? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat May 15, 2021 11:14 pm |
|
|
Seriously, a hugely complicated way to do thing....
Code: |
char hexdigits[10]; //male sure this is large enough.....
int32 intpart;
doubleWeigth = 695.581;
intpart = doubleWeight; //This now contains just the integer part
//intpart will now be 695
itos(intpart, 16, hexdigits);
|
hexdigits[0] will be '2', [1] will be 'b', [2] will be '7'. [3] will be '\0'.
This allows the conversion maths to be done in integer. Trying to do this as
float will be slow and inaccurate.
You need to #include <stdlib.h> before this is available.
Multiplying by 100, meant your intpart value was 69500. So the
[0] digit would always be 0. Then %1 would effectively only give binary
for the second and third part of your result. Not what you want....
Then your handling of values above 10, was fundamentally flawed.
You are generating the number 20, for a digit of 14, which is not either
ASCII or correct in decimal. 14 wants to be ASCII 'E', or the number
14.
Remember hex is an ASCII encoding, so 0 needs to be '0'.
If you just want base 16 'digits', not hex, then simply use %16, and
then divide by 16 for each hex value, till the remaining value is zero. |
|
|
delilcodes
Joined: 12 May 2021 Posts: 5
|
|
Posted: Sun May 16, 2021 1:04 am |
|
|
dluu13 wrote: | Are you trying to store each hex digit as an element of an array? |
Yeah. I wanna create modbus communication and send value of weigth to master for respond. I wanted to send hex digits but it is does not make sense. So I simply wanna send only digits no matter is hex or dec.
Thanks for reply. You are soo right. I got so confused while coding.
I will use your way like:
Code: |
int16 intPart; // i think its enough for 3 digit number
int8 fracPart; // i think this is enough for 2 digit number: i%100
int16 weigthDigits[4];
intPart = (doubleWeigth); // you are soo right :)
weigthDigits[0] = (intPart - intPart%100)/100;
weigthDigits[1] = (intPart%100 - intPart%10)/10;
weigthDigits[2] = (intPart%10); |
But I need fractional numbers to send to master. tenths maybe hundredths
So I can do this like;
Code: |
//fracPart = (doubleWeigth*100)%100;//this is notworking cuz of doubleWeigth not integer. so:
int32 i; //this must be int32, cuz of (doubleWeigth*100)
i= doubleWeigth*100;
fracPart = i%100;
weigthDigits[3] = (fracPart - fracPart%10)/10;
weigthDigits[4] = fracPart%10;
|
Is this a reasonable structure?
This error occurs when I want to use itos.
Code: | *** Error 12 "modbus_loadcell_led_slave_2.c" Line 136(11,12): Undefined identifier -- itos |
edit: some parameters edited |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun May 16, 2021 1:31 am |
|
|
As I said:
Quote: |
You need to #include <stdlib.h> before this is available.
|
How you send fractional numbers will depend totally on what the device
you are talking to expects.
Modbus does not support this. Making it into hex, wastes digits. Modbus
by standard supports 165bit binary values. Normally to send a FP number,
what you do is send two 16bit binary numbers that together contain the
IEEE 32bit numeric value, and the second device combines these back
to a IEEE value.
The library ieeefloat.c contains the code to convert a MicroChip float
value to IEEE format. Then you would send the 32bit result as two 16bit
transfers.
The other way devices do this (which may be why you have the *100),
is to take the float, multiply by 100, and send the result of this as a 32
bit integer, again as two 16bit transfers.
This though again never involves sending as hex.
Honestly if the other device expects to receive the value as 32bit integer
*100, then you would be much better off writing the code in the PIC
to actually use the values in this format. If it is coming from a sensor
rethink the code that is actually generating the value so that it works in
this format. CCS does support using integers in this way, and this
results in much faster/smaller maths than using FP. |
|
|
delilcodes
Joined: 12 May 2021 Posts: 5
|
|
Posted: Sun May 16, 2021 4:52 am |
|
|
Ttelmah wrote: | As I said:
Quote: |
You need to #include <stdlib.h> before this is available.
|
How you send fractional numbers will depend totally on what the device
you are talking to expects.
Modbus does not support this. Making it into hex, wastes digits. Modbus
by standard supports 165bit binary values. Normally to send a FP number,
what you do is send two 16bit binary numbers that together contain the
IEEE 32bit numeric value, and the second device combines these back
to a IEEE value.
The library ieeefloat.c contains the code to convert a MicroChip float
value to IEEE format. Then you would send the 32bit result as two 16bit
transfers.
The other way devices do this (which may be why you have the *100),
is to take the float, multiply by 100, and send the result of this as a 32
bit integer, again as two 16bit transfers.
This though again never involves sending as hex.
Honestly if the other device expects to receive the value as 32bit integer
*100, then you would be much better off writing the code in the PIC
to actually use the values in this format. If it is coming from a sensor
rethink the code that is actually generating the value so that it works in
this format. CCS does support using integers in this way, and this
results in much faster/smaller maths than using FP. |
I understand. Thanks for your help. |
|
|
|