Compare Revisions

Working with data

Change Revisions

Revision 98267:

Revision 98267 by Sheppy on

Revision 98268:

Revision 98268 by Sheppy on

Title:
Working with data
Working with data
Slug:
Mozilla/js-ctypes/Using_js-ctypes/Working_with_data
Mozilla/js-ctypes/Using_js-ctypes/Working_with_data
Tags:
JavaScript, js-ctypes
JavaScript, js-ctypes
Content:

Revision 98267
Revision 98268
n144      C functions expect strings to be arrays of characters, withn144      C functions expect strings to be arrays of characters, with
> the end of the string indicated by a null character. JavaScript,> the end of the string indicated by a null character. JavaScript,
> on the other hand, uses the <a href="/en/Core_JavaScript_1.5_Ref> on the other hand, uses the <a href="/En/Core_JavaScript_1.5_Ref
>erence/Global_Objects/String" title="en/Core JavaScript 1.5 Refer>erence/Global_Objects/String" title="en/Core JavaScript 1.5 Refer
>ence/Global Objects/String"><code>String</code></a> object to rep>ence/Global Objects/String"><code>String</code></a> object to rep
>resent strings.>resent strings.
n150      The <a href="/en/js-ctypes/js-ctypes_reference/CData" titlen150      The <a href="/en/js-ctypes/js-ctypes_reference/CData" title
>="en/js-ctypes/js-ctypes reference/CData"><code>CData</code></a> >="en/js-ctypes/js-ctypes reference/CData"><code>CData</code></a> 
>object provides the <a href="/en/js-ctypes/js-ctypes_reference/CD>object provides the <a href="/en/js-ctypes/js-ctypes_reference/CD
>ata#readString()" title="en/js-ctypes/js-ctypes reference/CData#r>ata#readString()" title="en/js-ctypes/js-ctypes reference/CData#r
>eadString()"><code>readString()</code></a>&nbsp;method, which rea>eadString()"><code>readString()</code></a>&nbsp;method, which rea
>ds bytes from the specified string and returns a new JavaScript <>ds bytes from the specified string and returns a new JavaScript <
>a href="/en/Core_JavaScript_1.5_Reference/Global_Objects/String" >a href="/En/Core_JavaScript_1.5_Reference/Global_Objects/String" 
>title="en/Core JavaScript 1.5 Reference/Global Objects/String"><c>title="en/Core JavaScript 1.5 Reference/Global Objects/String"><c
>ode>String</code></a> object representing that string.>ode>String</code></a> object representing that string.
t165      <em>work in progress</em>t165      Happily, converting JavaScript strings to C formatted strin
 >gs is easy; just create a character array containing the JavaScri
 >pt string:
166    </p>
167    <pre>
168var myUTF8String = ctypes.char.array()("Original string.");
169</pre>
170    <p>
171      This creates a UTF-8 format null-terminated string in the c
 >haracter array named <code>myUTF8String</code>.
172    </p>
173    <p>
174      If you need a UTF-16 string, you can do this:
175    </p>
176    <pre>
177var myUTF16String = ctypes.jschar.array()("Original string.");
178</pre>
179    <div class="note">
180      <strong>Note:</strong> At this time, there's no way to spec
 >ify a particular encoding; you may only retrieve the string in UT
 >F-8 or UTF-16 as shown above.
181    </div>
182    <h3>
183      Using strings with C functions
184    </h3>
185    <p>
186      You don't even need to convert strings when using them as i
 >nput parameters to C functions. They get converted automatically 
 >for you. Just pass in the JavaScript <a href="/en/JavaScript/Refe
 >rence/Global_Objects/String" title="en/JavaScript/Reference/Globa
 >l Objects/String"><code>String</code></a> object.
187    </p>
188    <p>
189      However, when C functions return, they still return a <code
 >>char.ptr</code> or <code>jschar.ptr</code> (that is, a pointer t
 >o an 8-bit or 16-bit array of characters). You'll have to convert
 > those yourself, as covered above.

Back to History