Stacker Posted November 25, 2018 Share Posted November 25, 2018 Hi i need help with conversion of data received from serial. I read hex value 30 8C in binary 00110000 10001100 (16 bit) i need to have result decimal of 561 (i try all but i crash my head on the wall) so : adc send a 10 bit value with first number "30" as LSB and the MSB (2 bits) are the 2 most left (6-7) of second number in binary other bits 0-5 need to discard MSB LSB RESULT I NEED IS 10xxxxxxxx 00110000 10 00110000 i try with: Local $ret = _CommGetLine(@CR, 100, 1000) ; return 308C $retL =stringtobinary(StringLeft($ret,1)) ; get first char as LSB $retH = stringtobinary(StringRight($ret,1)) ; get second char $reth= Bitrotate($retl,2,"W") ; then try but nothing to do $ret = $reth+$retl ; this sum need to be 561 please help me thanks a lot Link to comment Share on other sites More sharing options...
Developers Jos Posted November 25, 2018 Developers Share Posted November 25, 2018 Moved to the appropriate forum, as the Developer General Discussion forum very clearly states: Quote General development and scripting discussions. If it's super geeky and you don't know where to put it - it's probably here. Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums. Moderation Team SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
TheXman Posted November 25, 2018 Share Posted November 25, 2018 (edited) If I understand you correctly, I come up with a value of 560 not 561. $xLittleEndianValue = Binary("0x308C") ;Split the 2 binary bytes into separate bytes (LSByte & MSByte) $xLSByte = BinaryMid($xLittleEndianValue, 1, 1) $xMSByte = BinaryMid($xLittleEndianValue, 2, 1) ;Shift the most significant byte's bits 6 bits to the right ;i.e. 10001100 (0x8C) -> 00000010 (0x02) $xShiftedMSByte = Binary("0x" & Hex(BitShift($xMSByte, 6), 2)) ;Concatenate the MSByte and LSByte back into its binary representation $xLittleEndianResult = Binary("0x" & Hex($xLSByte) & Hex($xShiftedMSByte)) ConsoleWrite(StringFormat("$xLittleEndianValue = %s", $xLittleEndianValue) & @CRLF) ConsoleWrite(StringFormat("$xLSByte = %s", $xLSByte) & @CRLF) ConsoleWrite(StringFormat("$xMSByte = %s", $xMSByte) & @CRLF) ConsoleWrite(StringFormat("$xShiftedMSByte = %s", $xShiftedMSByte) & @CRLF) ConsoleWrite(StringFormat("$xLittleEndianResult = %s / BigEndian = 0x%04X / Decimal = %i", $xLittleEndianResult, $xLittleEndianResult, $xLittleEndianResult) & @CRLF) Output: $xLittleEndianValue = 0x308C $xLSByte = 0x30 $xMSByte = 0x8C $xShiftedMSByte = 0x02 $xLittleEndianResult = 0x3002 / BigEndian = 0x0230 / Decimal = 560 Edited November 25, 2018 by TheXman Corrected my endian references. Stacker 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Stacker Posted November 25, 2018 Author Share Posted November 25, 2018 Thanks TheXman code work fine, i make mistake with calculator 560 is the correct value. Now i need to understand your code row by row with help file Link to comment Share on other sites More sharing options...
TheXman Posted November 25, 2018 Share Posted November 25, 2018 You're welcome. I added comments to the original snippet. Hopefully it helps you better understand what the code is doing. Stacker 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
TheXman Posted November 25, 2018 Share Posted November 25, 2018 (edited) Here's a much shorter way to do the conversion, just 1 line. $xLittleEndianValue = Binary("0x308C") $iValue = BitShift(Int($xLittleEndianValue), 6) ConsoleWrite(StringFormat("Initial Value = %s (Little-Endian)", $xLittleEndianValue) & @CRLF) ConsoleWrite(StringFormat("Result (Decimal) = %i", $iValue) & @CRLF) ConsoleWrite(StringFormat("Result (Little-Endian) = %s", BinaryMid($iValue, 1, 2)) & @CRLF) ConsoleWrite(StringFormat("Result (Big-Endian) = 0x%04X", $iValue) & @CRLF) Output Initial Value = 0x308C (Little-Endian) Result (Decimal) = 560 Result (Little-Endian) = 0x3002 Result (Big-Endian) = 0x0230 Edited November 26, 2018 by TheXman Fixed typo Stacker 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now