<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/</id><updated>2022-01-11T14:35:08.843000Z</updated><subtitle>Recent posts to Extract 4 bytes from array of bytes to save in decimal value (int?)</subtitle><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#2d8f/6368/ffa4/f53f" rel="alternate"/><published>2022-01-11T14:35:08.843000Z</published><updated>2022-01-11T14:35:08.843000Z</updated><author><name>sgronchi</name><uri>https://forge.codesys.com/u/sgronchi/</uri></author><id>https://forge.codesys.comb19324fc93d0b7679aa28347635953852da551ef</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;The focus is on the readability, almost everyone can roll a (de)serialization code.&lt;/p&gt;
&lt;p&gt;A library function which is already made and does everything right and aptly named is way better than something that would be copypasted all over the place and quickly gets messy.&lt;/p&gt;
&lt;p&gt;Besides that, your code is broken on 16-bit processors. Yes, they still exist, run Codesys and continue to be deployed.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#2d8f/6368/ffa4" rel="alternate"/><published>2022-01-11T13:55:57.782000Z</published><updated>2022-01-11T13:55:57.782000Z</updated><author><name>nothinrandom</name><uri>https://forge.codesys.com/u/nothinrandom/</uri></author><id>https://forge.codesys.comd09359cafe7bff52b1868c27dfb9c458a2b8e56c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Oh good catch!  I didn't notice the system endianess.  Typically, I would avoid adding additional libraries just to use a single function.&lt;/p&gt;
&lt;p&gt;Let's get back to the basics where packing bytes is simply bit shifting.  In this case, the output could be computed via (assuming receiving system is little endian):&lt;/p&gt;
&lt;p&gt;dwOutput := SHL(Application.GVL_IO_LINK.Port1_inputs&lt;span&gt;[4]&lt;/span&gt;, 24) + SHL(Application.GVL_IO_LINK.Port1_inputs&lt;a class="alink" href="/forge/talk-ru/3/"&gt;[3]&lt;/a&gt;, 16) + SHL(Application.GVL_IO_LINK.Port1_inputs&lt;a class="alink" href="/forge/talk-ru/2/"&gt;[2]&lt;/a&gt;, 8) + Application.GVL_IO_LINK.Port1_inputs&lt;a class="alink" href="/forge/talk-ru/1/"&gt;[1]&lt;/a&gt;;&lt;/p&gt;
&lt;p&gt;Bit shifting is the equivalent of multiplying by orders of 2 (e.g. shift left by 1 means multiply by 2).&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#2d8f/6368" rel="alternate"/><published>2022-01-07T15:10:23.976000Z</published><updated>2022-01-07T15:10:23.976000Z</updated><author><name>sgronchi</name><uri>https://forge.codesys.com/u/sgronchi/</uri></author><id>https://forge.codesys.com89adaabb8f8328e50a87b3226958060bc7cc51db</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;This works only if you have a big endian machine.&lt;br/&gt;
The easiest way would be using the dedicated functions from CAA Memory.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#2d8f" rel="alternate"/><published>2022-01-02T19:10:25.032000Z</published><updated>2022-01-02T19:10:25.032000Z</updated><author><name>nothinrandom</name><uri>https://forge.codesys.com/u/nothinrandom/</uri></author><id>https://forge.codesys.com6220a44a142a8b55499a787d0a42724e32d25c40</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;a class="user-mention" href="/u/alberth007/"&gt;@alberth007&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Probably the easiest and most efficient way is to use pointer.  Just create a pointer to DWORD since it's 4 bytes.  For example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt;
&lt;span class="n"&gt;pdwValue&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;POINTER&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;DWORD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;memory&lt;/span&gt; &lt;span class="n"&gt;address&lt;/span&gt;
&lt;span class="n"&gt;pdwValue&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ADR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GVL_IO_LINK&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Port1_inputs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;dereference&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;operate&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;directly&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdwValue&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt;
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="n"&gt;something&lt;/span&gt;
&lt;span class="n"&gt;END_IF&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="k"&gt;less&lt;/span&gt; &lt;span class="n"&gt;efficient&lt;/span&gt; &lt;span class="n"&gt;way&lt;/span&gt; &lt;span class="n"&gt;would&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="k"&gt;copy&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;another&lt;/span&gt; &lt;span class="k"&gt;variable&lt;/span&gt;
&lt;span class="n"&gt;myValue&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pdwValue&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#8827/1d6e/d6d9" rel="alternate"/><published>2021-12-02T10:46:13.329000Z</published><updated>2021-12-02T10:46:13.329000Z</updated><author><name>sgronchi</name><uri>https://forge.codesys.com/u/sgronchi/</uri></author><id>https://forge.codesys.com14e6936c6631e08e0cc3c8aae3e24e4af80361ab</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Here you are: &lt;a href="https://help.codesys.com/webapp/JMeheH6MvY4aZSJh5TgzrbLOrZE%2FPackBytesToDword;product=CAA%20Memory;version=3.5.17.0" rel="nofollow"&gt;https://help.codesys.com/webapp/JMeheH6MvY4aZSJh5TgzrbLOrZE%2FPackBytesToDword;product=CAA%20Memory;version=3.5.17.0&lt;/a&gt;&lt;br/&gt;
It is just the Codesys WebHelp, you could have searched for PackBytesToDword.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#8827/1d6e" rel="alternate"/><published>2021-11-29T17:51:47.652000Z</published><updated>2021-11-29T17:51:47.652000Z</updated><author><name>vvora</name><uri>https://forge.codesys.com/u/vvora/</uri></author><id>https://forge.codesys.com5224953176262a563c752a5b68d90f7a432f201c</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hi, can you re-share the link. This one seems to be inactive at the moment. &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#4800/43e3" rel="alternate"/><published>2021-04-26T06:00:59.238000Z</published><updated>2021-04-26T06:00:59.238000Z</updated><author><name>sgronchi</name><uri>https://forge.codesys.com/u/sgronchi/</uri></author><id>https://forge.codesys.comd5e7d7f874e0f217c1bc3cbf71464088e8a5b8e9</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;You should recheck your coefficients. Or just use the PackBytesToDWord function which is more explicative in source code.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=250#4800" rel="alternate"/><published>2021-04-24T22:50:17.032000Z</published><updated>2021-04-24T22:50:17.032000Z</updated><author><name>Singh</name><uri>https://forge.codesys.com/u/baldeep/</uri></author><id>https://forge.codesys.com8b57816c1b1e16ea3d37ef0efcb284cc39beec62</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;0&lt;em&gt;65536+0&lt;/em&gt;4096+1*256+182=437&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#8827" rel="alternate"/><published>2021-04-23T10:27:21.159000Z</published><updated>2021-04-23T10:27:21.159000Z</updated><author><name>sgronchi</name><uri>https://forge.codesys.com/u/sgronchi/</uri></author><id>https://forge.codesys.comd6bc0c7dfa05f074b3f56c711499e0815e673887</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Use CAA Memory PackBytesToDword function: &lt;a href="https://help.codesys.com/webapp/s6roH8o4mLnj5nlRX9QvTFobNAo%2FPackBytesToDword;product=CAA_Memory;version=3.5.12.0" rel="nofollow"&gt;https://help.codesys.com/webapp/s6roH8o4mLnj5nlRX9QvTFobNAo%2FPackBytesToDword;product=CAA_Memory;version=3.5.12.0&lt;/a&gt;&lt;br/&gt;
optionally followed by a DWORD_TO_DINT cast if you have a signed value in two's complement.&lt;/p&gt;
&lt;p&gt;Also, INT is two bytes. You want DINT (4 bytes).&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>Extract 4 bytes from array of bytes to save in decimal value (int?)</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/afca2f74ac/?limit=25#adf5" rel="alternate"/><published>2021-04-22T16:09:56.835000Z</published><updated>2021-04-22T16:09:56.835000Z</updated><author><name>alberth007</name><uri>https://forge.codesys.com/u/alberth007/</uri></author><id>https://forge.codesys.com31746e0b5d0c80e904a2b54afa8a857abbea1fe2</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I have a question, maybe easy for you, but I cannot solve it and maybe you can help. I have a sensor that provides me the data in an array of 16 bytes. I need to take the 4 first bytes and save it in an INT value which is a distance value, so I have in the four first bytes &lt;span&gt;[0,0,1,181,..]&lt;/span&gt; which is 437 mm that my sensor is displaying I need these 437 into a variable (INT I suppose). How can I do it easily?&lt;/p&gt;
&lt;p&gt;Thanks for your help!&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>