0
Under review

String.fromCharCode(0) problem

Dmitry Ryzhkov 8 years ago in iRidium Script updated by Ekaterina (head of support) 8 years ago 1

Hello


I'm use yours function from dev portal, see below. When 0 appears in array sending string don't have any characters and stop sending.


In my case I use next procedure. I will need transform length string to 4 byte present and send it to device.



var value = "0";
//This not working when coming 0
var elementLength = String.fromCharCode(value.length >> 24 & 0xFF)+
                    String.fromCharCode(value.length >> 16 & 0xFF)+
                    String.fromCharCode(value.length >>  8 & 0xFF)+
                    String.fromCharCode(value.length       & 0xFF);
// This working fine witch concat
var elementLength = [ 
                      value.length >> 24 & 0xFF,
                      value.length >> 16 & 0xFF,
                      value.length >> 08 & 0xFF,
                      value.length >> 00 & 0xFF

                    ];
<em>// process an array of HEX bytes from Data field of Command or Feedback</em>
<strong>function</strong> HexArrayToAsciiString<span class="br0">(</span>in_aArray<span class="br0">)</span> 
<span class="br0">{</span>
   <em>// init string</em>
   <strong>var</strong> l_sStr <span class="sy0">=</span> <span class="st0">''</span><span class="sy0">;</span>
 
   <em>// aray bypass</em>
   <strong>for</strong><span class="br0">(</span><strong>var</strong> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0"><</span> in_aArray.<span class="me1">length</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">)</span>
      l_sStr <span class="sy0">+=</span> <span class="kw4">String</span>.<span class="me1">fromCharCode</span><span class="br0">(</span>parseInt<span class="br0">(</span>in_aArray<span class="br0">[</span>i<span class="br0">]</span><span class="sy0">,</span> <span class="nu0">16</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
 
   <em>// return string in ASCII format</em>
   <strong>return</strong> l_sStr<span class="sy0">;</span>
<span class="br0">}</span>