File: String-Conversions.html

package info (click to toggle)
octave 3.6.2-5%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 71,636 kB
  • sloc: cpp: 241,186; fortran: 23,651; sh: 14,790; ansic: 7,153; lex: 3,761; objc: 3,404; yacc: 3,386; makefile: 2,073; awk: 985; perl: 838
file content (447 lines) | stat: -rw-r--r-- 20,564 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<html lang="en">
<head>
<title>String Conversions - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Strings.html#Strings" title="Strings">
<link rel="prev" href="Manipulating-Strings.html#Manipulating-Strings" title="Manipulating Strings">
<link rel="next" href="Character-Class-Functions.html#Character-Class-Functions" title="Character Class Functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="String-Conversions"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Character-Class-Functions.html#Character-Class-Functions">Character Class Functions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Manipulating-Strings.html#Manipulating-Strings">Manipulating Strings</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Strings.html#Strings">Strings</a>
<hr>
</div>

<h3 class="section">5.6 String Conversions</h3>

<p>Octave supports various kinds of conversions between strings and
numbers.  As an example, it is possible to convert a string containing
a hexadecimal number to a floating point number.

<pre class="example">     hex2dec ("FF")
          &rArr; ans = 255
</pre>
   <!-- bin2dec scripts/strings/bin2dec.m -->
   <p><a name="doc_002dbin2dec"></a>

<div class="defun">
&mdash; Function File:  <b>bin2dec</b> (<var>s</var>)<var><a name="index-bin2dec-396"></a></var><br>
<blockquote><p>Return the decimal number corresponding to the binary number represented
by the string <var>s</var>.  For example:

     <pre class="example">          bin2dec ("1110")
               &rArr; 14
</pre>
        <p>Spaces are ignored during conversion and may be used to make the binary
number more readable.

     <pre class="example">          bin2dec ("1000 0001")
               &rArr; 129
</pre>
        <p>If <var>s</var> is a string matrix, return a column vector with one converted
number per row of <var>s</var>; Invalid rows evaluate to NaN.

        <p>If <var>s</var> is a cell array of strings, return a column vector with one
converted number per cell element in <var>s</var>. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002ddec2bin.html#doc_002ddec2bin">dec2bin</a>, <a href="doc_002dbase2dec.html#doc_002dbase2dec">base2dec</a>, <a href="doc_002dhex2dec.html#doc_002dhex2dec">hex2dec</a>. 
</p></blockquote></div>

<!-- dec2bin scripts/strings/dec2bin.m -->
   <p><a name="doc_002ddec2bin"></a>

<div class="defun">
&mdash; Function File:  <b>dec2bin</b> (<var>d, len</var>)<var><a name="index-dec2bin-397"></a></var><br>
<blockquote><p>Return a binary number corresponding to the non-negative integer
<var>d</var>, as a string of ones and zeros.  For example:

     <pre class="example">          dec2bin (14)
               &rArr; "1110"
</pre>
        <p>If <var>d</var> is a matrix or cell array, return a string matrix with one
row per element in <var>d</var>, padded with leading zeros to the width of
the largest value.

        <p>The optional second argument, <var>len</var>, specifies the minimum
number of digits in the result. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dbin2dec.html#doc_002dbin2dec">bin2dec</a>, <a href="doc_002ddec2base.html#doc_002ddec2base">dec2base</a>, <a href="doc_002ddec2hex.html#doc_002ddec2hex">dec2hex</a>. 
</p></blockquote></div>

<!-- dec2hex scripts/strings/dec2hex.m -->
   <p><a name="doc_002ddec2hex"></a>

<div class="defun">
&mdash; Function File:  <b>dec2hex</b> (<var>d, len</var>)<var><a name="index-dec2hex-398"></a></var><br>
<blockquote><p>Return the hexadecimal string corresponding to the non-negative
integer <var>d</var>.  For example:

     <pre class="example">          dec2hex (2748)
               &rArr; "ABC"
</pre>
        <p>If <var>d</var> is a matrix or cell array, return a string matrix with one
row per element in <var>d</var>, padded with leading zeros to the width of
the largest value.

        <p>The optional second argument, <var>len</var>, specifies the minimum
number of digits in the result. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dhex2dec.html#doc_002dhex2dec">hex2dec</a>, <a href="doc_002ddec2base.html#doc_002ddec2base">dec2base</a>, <a href="doc_002ddec2bin.html#doc_002ddec2bin">dec2bin</a>. 
</p></blockquote></div>

<!-- hex2dec scripts/strings/hex2dec.m -->
   <p><a name="doc_002dhex2dec"></a>

<div class="defun">
&mdash; Function File:  <b>hex2dec</b> (<var>s</var>)<var><a name="index-hex2dec-399"></a></var><br>
<blockquote><p>Return the integer corresponding to the hexadecimal number represented
by the string <var>s</var>.  For example:

     <pre class="example">          hex2dec ("12B")
               &rArr; 299
          hex2dec ("12b")
               &rArr; 299
</pre>
        <p>If <var>s</var> is a string matrix, return a column vector with one converted
number per row of <var>s</var>; Invalid rows evaluate to NaN.

        <p>If <var>s</var> is a cell array of strings, return a column vector with one
converted number per cell element in <var>s</var>.

     <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002ddec2hex.html#doc_002ddec2hex">dec2hex</a>, <a href="doc_002dbase2dec.html#doc_002dbase2dec">base2dec</a>, <a href="doc_002dbin2dec.html#doc_002dbin2dec">bin2dec</a>. 
</p></blockquote></div>

<!-- dec2base scripts/strings/dec2base.m -->
   <p><a name="doc_002ddec2base"></a>

<div class="defun">
&mdash; Function File:  <b>dec2base</b> (<var>d, base</var>)<var><a name="index-dec2base-400"></a></var><br>
&mdash; Function File:  <b>dec2base</b> (<var>d, base, len</var>)<var><a name="index-dec2base-401"></a></var><br>
<blockquote><p>Return a string of symbols in base <var>base</var> corresponding to
the non-negative integer <var>d</var>.

     <pre class="example">          dec2base (123, 3)
             &rArr; "11120"
</pre>
        <p>If <var>d</var> is a matrix or cell array, return a string matrix with one
row per element in <var>d</var>, padded with leading zeros to the width of
the largest value.

        <p>If <var>base</var> is a string then the characters of <var>base</var> are used as
the symbols for the digits of <var>d</var>.  Space (' ') may not be used
as a symbol.

     <pre class="example">          dec2base (123, "aei")
             &rArr; "eeeia"
</pre>
        <p>The optional third argument, <var>len</var>, specifies the minimum
number of digits in the result. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dbase2dec.html#doc_002dbase2dec">base2dec</a>, <a href="doc_002ddec2bin.html#doc_002ddec2bin">dec2bin</a>, <a href="doc_002ddec2hex.html#doc_002ddec2hex">dec2hex</a>. 
</p></blockquote></div>

<!-- base2dec scripts/strings/base2dec.m -->
   <p><a name="doc_002dbase2dec"></a>

<div class="defun">
&mdash; Function File:  <b>base2dec</b> (<var>s, base</var>)<var><a name="index-base2dec-402"></a></var><br>
<blockquote><p>Convert <var>s</var> from a string of digits in base <var>base</var> to a decimal
integer (base 10).

     <pre class="example">          base2dec ("11120", 3)
             &rArr; 123
</pre>
        <p>If <var>s</var> is a string matrix, return a column vector with one value per
row of <var>s</var>.  If a row contains invalid symbols then the
corresponding value will be NaN.

        <p>If <var>s</var> is a cell array of strings, return a column vector with one
value per cell element in <var>s</var>.

        <p>If <var>base</var> is a string, the characters of <var>base</var> are used as the
symbols for the digits of <var>s</var>.  Space (' ') may not be used as a
symbol.

     <pre class="example">          base2dec ("yyyzx", "xyz")
             &rArr; 123
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002ddec2base.html#doc_002ddec2base">dec2base</a>, <a href="doc_002dbin2dec.html#doc_002dbin2dec">bin2dec</a>, <a href="doc_002dhex2dec.html#doc_002dhex2dec">hex2dec</a>. 
</p></blockquote></div>

<!-- num2hex src/DLD-FUNCTIONS/hex2num.cc -->
   <p><a name="doc_002dnum2hex"></a>

<div class="defun">
&mdash; Loadable Function: <var>s</var> = <b>num2hex</b> (<var>n</var>)<var><a name="index-num2hex-403"></a></var><br>
<blockquote><p>Typecast a double precision number or vector to a 16 character hexadecimal
string of the IEEE 754 representation of the number.  For example:

     <pre class="example">          num2hex ([-1, 1, e, Inf, NaN, NA]);
          &rArr; "bff0000000000000
              3ff0000000000000
              4005bf0a8b145769
              7ff0000000000000
              fff8000000000000
              7ff00000000007a2"
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dhex2num.html#doc_002dhex2num">hex2num</a>, <a href="doc_002dhex2dec.html#doc_002dhex2dec">hex2dec</a>, <a href="doc_002ddec2hex.html#doc_002ddec2hex">dec2hex</a>. 
</p></blockquote></div>

<!-- hex2num src/DLD-FUNCTIONS/hex2num.cc -->
   <p><a name="doc_002dhex2num"></a>

<div class="defun">
&mdash; Loadable Function: <var>n</var> = <b>hex2num</b> (<var>s</var>)<var><a name="index-hex2num-404"></a></var><br>
<blockquote><p>Typecast the 16 character hexadecimal character string to an IEEE 754
double precision number.  If fewer than 16 characters are given the
strings are right padded with '0' characters.

        <p>Given a string matrix, <code>hex2num</code> treats each row as a separate
number.

     <pre class="example">          hex2num (["4005bf0a8b145769";"4024000000000000"])
          &rArr; [2.7183; 10.000]
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dnum2hex.html#doc_002dnum2hex">num2hex</a>, <a href="doc_002dhex2dec.html#doc_002dhex2dec">hex2dec</a>, <a href="doc_002ddec2hex.html#doc_002ddec2hex">dec2hex</a>. 
</p></blockquote></div>

<!-- str2double src/DLD-FUNCTIONS/str2double.cc -->
   <p><a name="doc_002dstr2double"></a>

<div class="defun">
&mdash; Built-in Function:  <b>str2double</b> (<var>s</var>)<var><a name="index-str2double-405"></a></var><br>
<blockquote><p>Convert a string to a real or complex number.

        <p>The string must be in one of the following formats where
a and b are real numbers and the complex unit is 'i' or 'j':

          <ul>
<li>a + bi

          <li>a + b*i

          <li>a + i*b

          <li>bi + a

          <li>b*i + a

          <li>i*b + a
</ul>

        <p>If present, a and/or b are of the form [+-]d[,.]d[[eE][+-]d] where
the brackets indicate optional arguments and 'd' indicates zero or more
digits.  The special input values <code>Inf</code>, <code>NaN</code>, and <code>NA</code> are
also accepted.

        <p><var>s</var> may also be a character matrix, in which case the conversion is
repeated for each row.  Or <var>s</var> may be a cell array of strings, in which
case each element is converted and an array of the same dimensions is
returned.

        <p><code>str2double</code> returns NaN for elements of <var>s</var> which cannot be
converted.

        <p><code>str2double</code> can replace <code>str2num</code>, and it avoids the security
risk of using <code>eval</code> on unknown data. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dstr2num.html#doc_002dstr2num">str2num</a>. 
</p></blockquote></div>

<!-- strjust scripts/strings/strjust.m -->
   <p><a name="doc_002dstrjust"></a>

<div class="defun">
&mdash; Function File:  <b>strjust</b> (<var>s</var>)<var><a name="index-strjust-406"></a></var><br>
&mdash; Function File:  <b>strjust</b> (<var>s, pos</var>)<var><a name="index-strjust-407"></a></var><br>
<blockquote><p>Return the text, <var>s</var>, justified according to <var>pos</var>, which may
be &lsquo;<samp><span class="samp">"left"</span></samp>&rsquo;, &lsquo;<samp><span class="samp">"center"</span></samp>&rsquo;, or &lsquo;<samp><span class="samp">"right"</span></samp>&rsquo;.  If <var>pos</var>
is omitted it defaults to &lsquo;<samp><span class="samp">"right"</span></samp>&rsquo;.

        <p>Null characters are replaced by spaces.  All other character
data are treated as non-white space.

        <p>Example:

     <pre class="example">          strjust (["a"; "ab"; "abc"; "abcd"])
               &rArr;
                  "   a"
                  "  ab"
                  " abc"
                  "abcd"
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002ddeblank.html#doc_002ddeblank">deblank</a>, <a href="doc_002dstrrep.html#doc_002dstrrep">strrep</a>, <a href="doc_002dstrtrim.html#doc_002dstrtrim">strtrim</a>, <a href="doc_002duntabify.html#doc_002duntabify">untabify</a>. 
</p></blockquote></div>

<!-- str2num scripts/strings/str2num.m -->
   <p><a name="doc_002dstr2num"></a>

<div class="defun">
&mdash; Function File: <var>x</var> = <b>str2num</b> (<var>s</var>)<var><a name="index-str2num-408"></a></var><br>
&mdash; Function File: [<var>x</var>, <var>state</var>] = <b>str2num</b> (<var>s</var>)<var><a name="index-str2num-409"></a></var><br>
<blockquote><p>Convert the string (or character array) <var>s</var> to a number (or an
array).  Examples:

     <pre class="example">          str2num ("3.141596")
                &rArr; 3.141596
          
          str2num (["1, 2, 3"; "4, 5, 6"])
                &rArr; 1  2  3
                   4  5  6
</pre>
        <p>The optional second output, <var>state</var>, is logically true when the
conversion is successful.  If the conversion fails the numeric output,
<var>x</var>, is empty and <var>state</var> is false.

        <p><strong>Caution:</strong> As <code>str2num</code> uses the <code>eval</code> function
to do the conversion, <code>str2num</code> will execute any code contained
in the string <var>s</var>.  Use <code>str2double</code> for a safer and faster
conversion.

        <p>For cell array of strings use <code>str2double</code>. 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dstr2double.html#doc_002dstr2double">str2double</a>, <a href="doc_002deval.html#doc_002deval">eval</a>. 
</p></blockquote></div>

<!-- toascii src/mappers.cc -->
   <p><a name="doc_002dtoascii"></a>

<div class="defun">
&mdash; Mapping Function:  <b>toascii</b> (<var>s</var>)<var><a name="index-toascii-410"></a></var><br>
<blockquote><p>Return ASCII representation of <var>s</var> in a matrix.  For example:

     <pre class="example">          toascii ("ASCII")
               &rArr; [ 65, 83, 67, 73, 73 ]
          
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dchar.html#doc_002dchar">char</a>. 
</p></blockquote></div>

<!-- tolower src/mappers.cc -->
   <p><a name="doc_002dtolower"></a>

<div class="defun">
&mdash; Mapping Function:  <b>tolower</b> (<var>s</var>)<var><a name="index-tolower-411"></a></var><br>
&mdash; Mapping Function:  <b>lower</b> (<var>s</var>)<var><a name="index-lower-412"></a></var><br>
<blockquote><p>Return a copy of the string or cell string <var>s</var>, with each uppercase
character replaced by the corresponding lowercase one; non-alphabetic
characters are left unchanged.  For example:

     <pre class="example">          tolower ("MiXeD cAsE 123")
               &rArr; "mixed case 123"
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dtoupper.html#doc_002dtoupper">toupper</a>. 
</p></blockquote></div>

<!-- toupper src/mappers.cc -->
   <p><a name="doc_002dtoupper"></a>

<div class="defun">
&mdash; Mapping Function:  <b>toupper</b> (<var>s</var>)<var><a name="index-toupper-413"></a></var><br>
&mdash; Mapping Function:  <b>upper</b> (<var>s</var>)<var><a name="index-upper-414"></a></var><br>
<blockquote><p>Return a copy of the string or cell string <var>s</var>, with each lowercase
character replaced by the corresponding uppercase one; non-alphabetic
characters are left unchanged.  For example:

     <pre class="example">          toupper ("MiXeD cAsE 123")
               &rArr; "MIXED CASE 123"
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002dtolower.html#doc_002dtolower">tolower</a>. 
</p></blockquote></div>

<!-- do_string_escapes src/utils.cc -->
   <p><a name="doc_002ddo_005fstring_005fescapes"></a>

<div class="defun">
&mdash; Built-in Function:  <b>do_string_escapes</b> (<var>string</var>)<var><a name="index-do_005fstring_005fescapes-415"></a></var><br>
<blockquote><p>Convert special characters in <var>string</var> to their escaped forms. 
</p></blockquote></div>

<!-- undo_string_escapes src/utils.cc -->
   <p><a name="doc_002dundo_005fstring_005fescapes"></a>

<div class="defun">
&mdash; Built-in Function:  <b>undo_string_escapes</b> (<var>s</var>)<var><a name="index-undo_005fstring_005fescapes-416"></a></var><br>
<blockquote><p>Convert special characters in strings back to their escaped forms.  For
example, the expression

     <pre class="example">          bell = "\a";
</pre>
        <p class="noindent">assigns the value of the alert character (control-g, ASCII code 7) to
the string variable <code>bell</code>.  If this string is printed, the
system will ring the terminal bell (if it is possible).  This is
normally the desired outcome.  However, sometimes it is useful to be
able to print the original representation of the string, with the
special characters replaced by their escape sequences.  For example,

     <pre class="example">          octave:13&gt; undo_string_escapes (bell)
          ans = \a
</pre>
        <p class="noindent">replaces the unprintable alert character with its printable
representation. 
</p></blockquote></div>

   </body></html>