<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1435153954547_32533"><span>Thanks again...</span></div><div id="yui_3_16_0_1_1435153954547_32535"><span>I seem to not been able to grasp the idea and would appreciate a code example.</span></div><div id="yui_3_16_0_1_1435153954547_32087">For example byte1=0x07, byte2=0xDC, byte3=0xC0</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr">If I put these in excel and calculate 7*256*256 + 220*256 + 192 I get 520264.</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr">Then when I divide this by 10000, I get 52.0264. And this matches the 52% shown by the car.</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr"><br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr">Currently my code for this conversion is:</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv1 = (unsigned long)can_databuffer[4];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv1 = sv1*65536UL;<br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv2 = (unsigned long)can_databuffer[5];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv2 = sv2*256UL;<br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv3 = (unsigned long)can_databuffer[6];</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> sv3 = sv3;<br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> </div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""> car_SOC = (char)((unsigned long)(sv1 + sv2 + sv3 + 5000UL)/10000UL);<br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">And the result for car_SOC is 49. </div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">How is this possible?</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">And what would be the correct way to put these 3 bytes together and divide them by 10000 to get to the final value in char?</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br></div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">Thanks in advance,</div><div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style="">Nikolay</div><div dir="ltr" class="" style="" id="yui_3_16_0_1_1435153954547_33497"><br class="" style=""></div> <div id="yui_3_16_0_1_1435153954547_32087" dir="ltr" class="" style=""><br class="" style=""></div><br><div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" style="display: block;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;"> <div dir="ltr"> <font size="2" face="Arial"> On Monday, June 22, 2015 3:58 PM, Mark Webb-Johnson <mark@webb-johnson.net> wrote:<br> </font> </div> <br><br> <div class="y_msg_container"><div id="yiv7764699889"><div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div>Division is kind of nasty in 8bit. Actually very nasty. Unless you are dividing by 2, 4, 8, 16, etc. Divide by 2^N is the same as >>N, and that is the shortcut you want to do if at all possible.<div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">That said, your solution seems ok, except the value[2] should be <<8, and you probably want to add 5000 just before divide by 10000, to round correctly.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">If you don’t need too much precision, you might find an inaccurate kludge may be good enough.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">For example:</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><blockquote class="yiv7764699889" style="margin:0 0 0 40px;border:none;padding:0px;"><div class="yiv7764699889">205/2048 = 0.1000976562</div></blockquote><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">and that is pretty close to 0.1. So multiply by 205, then shirt right 11 spaces is pretty close to divide by 10. Multiplication is much less expensive than division.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">If you look at MiFromKm and KmFromMi in utils.c, you can see similar sorts of approximations to avoid big divisions.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889">Regards, Mark.</div><div class="yiv7764699889"><br clear="none" class="yiv7764699889"><div><blockquote class="yiv7764699889" type="cite"><div class="yiv7764699889yqt8228485529" id="yiv7764699889yqt72105"><div class="yiv7764699889">On 22 Jun, 2015, at 4:30 pm, Nikolay Shishkov <<a rel="nofollow" shape="rect" class="yiv7764699889" ymailto="mailto:nshishkov@yahoo.com" target="_blank" href="mailto:nshishkov@yahoo.com">nshishkov@yahoo.com</a>> wrote:</div><br clear="none" class="yiv7764699889Apple-interchange-newline"><div class="yiv7764699889"><div class="yiv7764699889"><div class="yiv7764699889" style="background-color:rgb(255, 255, 255);font-family:HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5352">I have a situation where I need to convert a value that is stored as 3 bytes to a char.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">The can be for example 7ED95, which is reported in 3 consequtive bytes 07, ED, 95.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">This value in decimal is 519573, which corresponds to a 51.9% SOC. </div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Is the proper way to do this conversion (from the 3 bytes to the value of 51) like this:</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">char soc = (char)(((unsigned long)value[1]<<16 + (unsigned long)value[2]<<16 + (unsigned long)value[3])/10000);</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">I have been doing stuff like that before with unions, but can't remember how either.</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Thanks,</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353"><br clear="none" class="yiv7764699889"></div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353">Nikolay</div><div class="yiv7764699889" dir="ltr" id="yiv7764699889yui_3_16_0_1_1434960628479_5353" style=""> </div></div></div>_______________________________________________<br clear="none" class="yiv7764699889">OvmsDev mailing list<br clear="none" class="yiv7764699889"><a rel="nofollow" shape="rect" class="yiv7764699889" ymailto="mailto:OvmsDev@lists.teslaclub.hk" target="_blank" href="mailto:OvmsDev@lists.teslaclub.hk">OvmsDev@lists.teslaclub.hk</a><br clear="none" class="yiv7764699889">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev<br clear="none" class="yiv7764699889"></div></div></blockquote></div><br clear="none" class="yiv7764699889"></div></div></div><br><div class="yqt8228485529" id="yqt18226">_______________________________________________<br clear="none">OvmsDev mailing list<br clear="none"><a shape="rect" ymailto="mailto:OvmsDev@lists.teslaclub.hk" href="mailto:OvmsDev@lists.teslaclub.hk">OvmsDev@lists.teslaclub.hk</a><br clear="none"><a shape="rect" href="http://lists.teslaclub.hk/mailman/listinfo/ovmsdev" target="_blank">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev</a><br clear="none"></div><br><br></div> </div> </div> </div></div></body></html>