<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></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=""><br class=""></div><div class="">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=""><br class=""></div><div class="">If you don’t need too much precision, you might find an inaccurate kludge may be good enough.</div><div class=""><br class=""></div><div class="">For example:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">205/2048 = 0.1000976562</div></blockquote><div class=""><br class=""></div><div class="">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=""><br class=""></div><div class="">If you look at MiFromKm and KmFromMi in utils.c, you can see similar sorts of approximations to avoid big divisions.</div><div class=""><br class=""></div><div class="">Regards, Mark.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 22 Jun, 2015, at 4:30 pm, Nikolay Shishkov <<a href="mailto:nshishkov@yahoo.com" class="">nshishkov@yahoo.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><div style="background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 16px;" class=""><div dir="ltr" id="yui_3_16_0_1_1434960628479_5352" class="">I have a situation where I need to convert a value that is stored as 3 bytes to a char.</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">The can be for example 7ED95, which is reported in 3 consequtive bytes 07, ED, 95.</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">This value in decimal is 519573, which corresponds to a 51.9% SOC. </div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">Is the proper way to do this conversion (from the 3 bytes to the value of 51) like this:</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class=""><br class=""></div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">char soc = (char)(((unsigned long)value[1]<<16 + (unsigned long)value[2]<<16 + (unsigned long)value[3])/10000);</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class=""><br class=""></div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">I have been doing stuff like that before with unions, but can't remember how either.</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class=""><br class=""></div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">Thanks,</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class=""><br class=""></div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="">Nikolay</div><div dir="ltr" id="yui_3_16_0_1_1434960628479_5353" class="" style=""> </div></div></div>_______________________________________________<br class="">OvmsDev mailing list<br class=""><a href="mailto:OvmsDev@lists.teslaclub.hk" class="">OvmsDev@lists.teslaclub.hk</a><br class="">http://lists.teslaclub.hk/mailman/listinfo/ovmsdev<br class=""></div></blockquote></div><br class=""></div></body></html>