{"id":821,"date":"2018-07-19T10:50:53","date_gmt":"2018-07-19T10:50:53","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=821"},"modified":"2022-02-17T10:13:11","modified_gmt":"2022-02-17T10:13:11","slug":"endian","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/endian","title":{"rendered":"Endian"},"content":{"rendered":"<p>\nEndianness defines the location of byte 0 within a larger data structure.\n<\/p>\n<p>\nLittle-endian\n<\/p>\n<p style=\"margin-left: 40px;\">\nThe least significant byte of a 16-bit value is sent or stored before the most significant byte\n<\/p>\n<p style=\"margin-left: 40px;\">\nBits7:0 | Bits15:8\n<\/p>\n<p style=\"margin-left: 40px;\">\nA 32bit value is stored in a byte array as:\n<\/p>\n<p style=\"margin-left: 80px;\">\nByte[3] | Byte[2] | Byte[1] | Byte[0]\n<\/p>\n<p>\nBig-endian\n<\/p>\n<p style=\"margin-left: 40px;\">\nThe more significant byte of a 16-bit value is sent or stored before the less significant byte\n<\/p>\n<p style=\"margin-left: 40px;\">\nBits15:8 | Bits7:0\n<\/p>\n<p style=\"margin-left: 40px;\">\nA 32bit value is stored in a byte array as:\n<\/p>\n<p style=\"margin-left: 80px;\">\nByte[0] | Byte[1] | Byte[2] | Byte[3]\n<\/p>\n<h4>\nEndian usage<br \/>\n<\/h4>\n<p>\nWindows &#8211; Little endian\n<\/p>\n<p>\nModbus&nbsp;&#8211; Big Endian\n<\/p>\n<h4>\nLittle&nbsp;Endian<br \/>\n<\/h4>\n<h5>\nUInt32&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[1] |&nbsp;UInt16[0]\n<\/p>\n<h5>\nUInt64&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[3] | UInt16[2] |&nbsp;UInt16[1] |&nbsp;UInt16[0]\n<\/p>\n<h4>\nBig Endian<br \/>\n<\/h4>\n<h5>\nUInt32&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] |&nbsp;UInt16[1]\n<\/p>\n<h5>\nUInt64&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] | UInt16[1] |&nbsp;UInt16[2] |&nbsp;UInt16[3]\n<\/p>\n<h5>\nFloat&nbsp;conversion<br \/>\n<\/h5>\n<p style=\"margin-left: 40px;\">\nUInt16[0] | UInt16[1]\n<\/p>\n<pre>\r\n<code>\r\n\t\/\/Converting big endian modbus registers to float on little endian windows&nbsp;(ModbusRegisterValues[0] was received first)\r\n\tarray&lt;Byte&gt;^byteArray = gcnew array&lt;Byte&gt;(4);\r\n\tbyteArray[3] = (Byte)(ModbusRegisterValues[0] &gt;&gt; 8);\r\n\tbyteArray[2] = (Byte)(ModbusRegisterValues[0] &amp; 0x00ff);\r\n\tbyteArray[1] = (Byte)(ModbusRegisterValues[1] &gt;&gt; 8);\r\n\tbyteArray[0] = (Byte)(ModbusRegisterValues[1] &amp; 0x00ff);\r\n\tfloat my_float = BitConverter::ToSingle(byteArray, 0);\r\n<\/code><\/pre>\n<pre>\r\n<code>\r\n\t\/\/Converting float on little endian windows to big endian modbus registers (ModbusRegisterValues[0] will be sent first)\r\n\tModbusRegisterValues = gcnew array &lt;UInt16&gt;(2);\r\n\tarray&lt;Byte&gt;^byteArray = BitConverter::GetBytes(my_float_value);\r\n\tModbusRegisterValues[0] = ((UInt16)byteArray[3] &lt;&lt; 8) | ((UInt16)byteArray[2]);\t\t\/\/Converting float on little endian windows to big endian modbus registers\r\n\tModbusRegisterValues[1] = ((UInt16)byteArray[1] &lt;&lt; 8) | ((UInt16)byteArray[0]);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Endianness defines the location of byte 0 within a larger data structure. Little-endian The least significant byte of a 16-bit value is sent or stored before the most significant byte Bits7:0 | Bits15:8 A 32bit value is stored in a byte array as: Byte[3] | Byte[2] | Byte[1] | Byte[0] Big-endian The more significant byte [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-821","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=821"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/821\/revisions"}],"predecessor-version":[{"id":822,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/821\/revisions\/822"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}