<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Array and Long  manipulations</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/16731/array-and-long-manipulations</link><description> Hi everyone, 
 
I have an array eg: 
 
 
command[9] = {0x3C , 0x4A , 0x33 , 0x25 , 0xB1 , 0xD3 , 0x59 , 0x34 , 0x2E }
 
 
the last 4 byte is the value (in hex) that I need to manipulate, let say increment by 3. 
 
and after manipulation I need to send</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: Array and Long  manipulations</title><link>https://community.arm.com/thread/110862?ContentTypeID=1</link><pubDate>Tue, 04 May 2004 14:57:57 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a2295b6e-2ef0-496a-9ded-ffa94a268cbc</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;i&gt;&amp;quot;If your data is actually structured, then why not structure it? ... If you do want to keep the command in a byte array...&amp;quot;&lt;/i&gt;&lt;br /&gt;
&lt;br /&gt;
You could use a &lt;b&gt;union&lt;/b&gt; - then you could have your cake as a &lt;b&gt;struct&lt;/b&gt; and eat it in bytes, too!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Array and Long  manipulations</title><link>https://community.arm.com/thread/96297?ContentTypeID=1</link><pubDate>Tue, 04 May 2004 11:11:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:b9b1391f-2601-41a7-a70b-72d5c86f0777</guid><dc:creator>Drew Davis</dc:creator><description>&lt;p&gt;Lots of ways to go about this.&lt;br /&gt;
&lt;br /&gt;
If your data is actually structured, then why not structure it?&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
typedef struct
    {
    CmdHeader header;  // 5 bytes
    U32       value;
    } Command;

Command command;

...
    command.value += 3;

    sendcom ((U8*)&amp;amp;command, sizeof(command));
&lt;/pre&gt;
&lt;br /&gt;
Just because the sendcom routine sends a series of bytes doesn&amp;#39;t mean the entire rest of your program needs to treat the data that way.&lt;br /&gt;
&lt;br /&gt;
Presumably that &amp;quot;CmdHeader&amp;quot; has some further structure, but your post doesn&amp;#39;t describe the data.&lt;br /&gt;
&lt;br /&gt;
If you do want to keep the command in a byte array, then you can also do some pointer surgery:&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;
U8 command[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
#define MyCmdValueOffset 5

...
   // find a pointer to value by adding 5
   // to the start of the command buffer,
   // and casting to long,
   *(long*)(command + MyCmdValueOffset) += 3;
&lt;/pre&gt;
&lt;br /&gt;
Note that this is exactly what the version with the struct does, except that the compiler does the work for you so that you do not have to manually maintain the value offset.&lt;br /&gt;
&lt;br /&gt;
sprintf() operates on strings.  It looks like you have an actual binary integer in that array.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Array and Long  manipulations</title><link>https://community.arm.com/thread/86769?ContentTypeID=1</link><pubDate>Tue, 04 May 2004 10:14:34 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:81a2c512-3858-42cd-ad77-b9fdc1dfc12c</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;Oops, in my first list item, make that command[5].&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Array and Long  manipulations</title><link>https://community.arm.com/thread/42154?ContentTypeID=1</link><pubDate>Tue, 04 May 2004 10:13:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:38b03f8a-066c-4fcb-b0fd-46fe7cb52cfc</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;ol&gt;&lt;li&gt;comand[9] (0xD3) is not an ASCII character&lt;/li&gt;
&lt;li&gt;k[], when supplied to strtol() is not big enough to hold the NUL-termintor that you are not even supplying to terminate the &amp;quot;string&amp;quot;&lt;/li&gt;
&lt;li&gt;The strtol() call&amp;#39;s third parameter (4) specifies base-4 conversion -- probably not what you want&lt;/li&gt;&lt;/ol&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>