<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: <a rel="next" accesskey="n" href="Character-Class-Functions.html#Character-Class-Functions">Character Class Functions</a>, Previous: <a rel="previous" accesskey="p" href="Manipulating-Strings.html#Manipulating-Strings">Manipulating Strings</a>, Up: <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") ⇒ ans = 255 </pre> <!-- bin2dec scripts/strings/bin2dec.m --> <p><a name="doc_002dbin2dec"></a> <div class="defun"> — 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") ⇒ 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") ⇒ 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"> — 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) ⇒ "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"> — 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) ⇒ "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"> — 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") ⇒ 299 hex2dec ("12b") ⇒ 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"> — Function File: <b>dec2base</b> (<var>d, base</var>)<var><a name="index-dec2base-400"></a></var><br> — 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) ⇒ "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") ⇒ "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"> — 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) ⇒ 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") ⇒ 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"> — 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]); ⇒ "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"> — 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"]) ⇒ [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"> — 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"> — Function File: <b>strjust</b> (<var>s</var>)<var><a name="index-strjust-406"></a></var><br> — 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 ‘<samp><span class="samp">"left"</span></samp>’, ‘<samp><span class="samp">"center"</span></samp>’, or ‘<samp><span class="samp">"right"</span></samp>’. If <var>pos</var> is omitted it defaults to ‘<samp><span class="samp">"right"</span></samp>’. <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"]) ⇒ " 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"> — Function File: <var>x</var> = <b>str2num</b> (<var>s</var>)<var><a name="index-str2num-408"></a></var><br> — 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") ⇒ 3.141596 str2num (["1, 2, 3"; "4, 5, 6"]) ⇒ 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"> — 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") ⇒ [ 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"> — Mapping Function: <b>tolower</b> (<var>s</var>)<var><a name="index-tolower-411"></a></var><br> — 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") ⇒ "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"> — Mapping Function: <b>toupper</b> (<var>s</var>)<var><a name="index-toupper-413"></a></var><br> — 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") ⇒ "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"> — 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"> — 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> undo_string_escapes (bell) ans = \a </pre> <p class="noindent">replaces the unprintable alert character with its printable representation. </p></blockquote></div> </body></html>