octave: String Conversions
5.6 String Conversions
======================
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.
hex2dec ("FF")
⇒ 255
-- : bin2dec (S)
Return the decimal number corresponding to the binary number
represented by the string S.
For example:
bin2dec ("1110")
⇒ 14
Spaces are ignored during conversion and may be used to make the
binary number more readable.
bin2dec ("1000 0001")
⇒ 129
If S is a string matrix, return a column vector with one converted
number per row of S; Invalid rows evaluate to NaN.
If S is a cell array of strings, return a column vector with one
converted number per cell element in S.
See also: dec2bin XREFdec2bin, base2dec XREFbase2dec,
hex2dec XREFhex2dec.
-- : dec2bin (D, LEN)
Return a binary number corresponding to the non-negative integer D,
as a string of ones and zeros.
For example:
dec2bin (14)
⇒ "1110"
If D is a matrix or cell array, return a string matrix with one row
per element in D, padded with leading zeros to the width of the
largest value.
The optional second argument, LEN, specifies the minimum number of
digits in the result.
See also: bin2dec XREFbin2dec, dec2base XREFdec2base,
dec2hex XREFdec2hex.
-- : dec2hex (D, LEN)
Return the hexadecimal string corresponding to the non-negative
integer D.
For example:
dec2hex (2748)
⇒ "ABC"
If D is a matrix or cell array, return a string matrix with one row
per element in D, padded with leading zeros to the width of the
largest value.
The optional second argument, LEN, specifies the minimum number of
digits in the result.
See also: hex2dec XREFhex2dec, dec2base XREFdec2base,
dec2bin XREFdec2bin.
-- : hex2dec (S)
Return the integer corresponding to the hexadecimal number
represented by the string S.
For example:
hex2dec ("12B")
⇒ 299
hex2dec ("12b")
⇒ 299
If S is a string matrix, return a column vector with one converted
number per row of S; Invalid rows evaluate to NaN.
If S is a cell array of strings, return a column vector with one
converted number per cell element in S.
See also: dec2hex XREFdec2hex, base2dec XREFbase2dec,
bin2dec XREFbin2dec.
-- : dec2base (D, BASE)
-- : dec2base (D, BASE, LEN)
Return a string of symbols in base BASE corresponding to the
non-negative integer D.
dec2base (123, 3)
⇒ "11120"
If D is a matrix or cell array, return a string matrix with one row
per element in D, padded with leading zeros to the width of the
largest value.
If BASE is a string then the characters of BASE are used as the
symbols for the digits of D. Space (’ ’) may not be used as a
symbol.
dec2base (123, "aei")
⇒ "eeeia"
The optional third argument, LEN, specifies the minimum number of
digits in the result.
See also: base2dec XREFbase2dec, dec2bin XREFdec2bin,
dec2hex XREFdec2hex.
-- : base2dec (S, BASE)
Convert S from a string of digits in base BASE to a decimal integer
(base 10).
base2dec ("11120", 3)
⇒ 123
If S is a string matrix, return a column vector with one value per
row of S. If a row contains invalid symbols then the corresponding
value will be NaN.
If S is a cell array of strings, return a column vector with one
value per cell element in S.
If BASE is a string, the characters of BASE are used as the symbols
for the digits of S. Space (’ ’) may not be used as a symbol.
base2dec ("yyyzx", "xyz")
⇒ 123
See also: dec2base XREFdec2base, bin2dec XREFbin2dec,
hex2dec XREFhex2dec.
-- : S = num2hex (N)
Typecast a double or single precision number or vector to a 8 or 16
character hexadecimal string of the IEEE 754 representation of the
number.
For example:
num2hex ([-1, 1, e, Inf])
⇒ "bff0000000000000
3ff0000000000000
4005bf0a8b145769
7ff0000000000000"
If the argument N is a single precision number or vector, the
returned string has a length of 8. For example:
num2hex (single ([-1, 1, e, Inf]))
⇒ "bf800000
3f800000
402df854
7f800000"
See also: hex2num XREFhex2num, hex2dec XREFhex2dec,
dec2hex XREFdec2hex.
-- : N = hex2num (S)
-- : N = hex2num (S, CLASS)
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.
Given a string matrix, ‘hex2num’ treats each row as a separate
number.
hex2num (["4005bf0a8b145769"; "4024000000000000"])
⇒ [2.7183; 10.000]
The optional argument CLASS can be passed as the string "single" to
specify that the given string should be interpreted as a single
precision number. In this case, S should be an 8 character
hexadecimal string. For example:
hex2num (["402df854"; "41200000"], "single")
⇒ [2.7183; 10.000]
See also: num2hex XREFnum2hex, hex2dec XREFhex2dec,
dec2hex XREFdec2hex.
-- : str2double (S)
Convert a string to a real or complex number.
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’:
• a + bi
• a + b*i
• a + i*b
• bi + a
• b*i + a
• i*b + a
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 ‘Inf’, ‘NaN’, and ‘NA’ are
also accepted.
S may be a character string, character matrix, or cell array. For
character arrays the conversion is repeated for every row, and a
double or complex array is returned. Empty rows in S are deleted
and not returned in the numeric array. For cell arrays each
character string element is processed and a double or complex array
of the same dimensions as S is returned.
For unconvertible scalar or character string input ‘str2double’
returns a NaN. Similarly, for character array input ‘str2double’
returns a NaN for any row of S that could not be converted. For a
cell array, ‘str2double’ returns a NaN for any element of S for
which conversion fails. Note that numeric elements in a mixed
string/numeric cell array are not strings and the conversion will
fail for these elements and return NaN.
‘str2double’ can replace ‘str2num’, and it avoids the security risk
of using ‘eval’ on unknown data.
See also: str2num XREFstr2num.
-- : strjust (S)
-- : strjust (S, POS)
Return the text, S, justified according to POS, which may be
"left", "center", or "right".
If POS is omitted it defaults to "right".
Null characters are replaced by spaces. All other character data
are treated as non-white space.
Example:
strjust (["a"; "ab"; "abc"; "abcd"])
⇒
" a"
" ab"
" abc"
"abcd"
See also: deblank XREFdeblank, strrep XREFstrrep,
strtrim XREFstrtrim, untabify XREFuntabify.
-- : X = str2num (S)
-- : [X, STATE] = str2num (S)
Convert the string (or character array) S to a number (or an
array).
Examples:
str2num ("3.141596")
⇒ 3.141596
str2num (["1, 2, 3"; "4, 5, 6"])
⇒ 1 2 3
4 5 6
The optional second output, STATE, is logically true when the
conversion is successful. If the conversion fails the numeric
output, X, is empty and STATE is false.
*Caution:* As ‘str2num’ uses the ‘eval’ function to do the
conversion, ‘str2num’ will execute any code contained in the string
S. Use ‘str2double’ for a safer and faster conversion.
For cell array of strings use ‘str2double’.
See also: str2double XREFstr2double, eval XREFeval.
-- : toascii (S)
Return ASCII representation of S in a matrix.
For example:
toascii ("ASCII")
⇒ [ 65, 83, 67, 73, 73 ]
See also: char XREFchar.
-- : tolower (S)
-- : lower (S)
Return a copy of the string or cell string S, with each uppercase
character replaced by the corresponding lowercase one;
non-alphabetic characters are left unchanged.
For example:
tolower ("MiXeD cAsE 123")
⇒ "mixed case 123"
See also: toupper XREFtoupper.
-- : toupper (S)
-- : upper (S)
Return a copy of the string or cell string S, with each lowercase
character replaced by the corresponding uppercase one;
non-alphabetic characters are left unchanged.
For example:
toupper ("MiXeD cAsE 123")
⇒ "MIXED CASE 123"
See also: tolower XREFtolower.
-- : do_string_escapes (STRING)
Convert escape sequences in STRING to the characters they
represent.
Escape sequences begin with a leading backslash (’\’) followed by
1–3 characters (.e.g., "\n" => newline).
See also: undo_string_escapes XREFundo_string_escapes.
-- : undo_string_escapes (S)
Convert special characters in strings back to their escaped forms.
For example, the expression
bell = "\a";
assigns the value of the alert character (control-g, ASCII code 7)
to the string variable ‘bell’. 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,
octave:13> undo_string_escapes (bell)
ans = \a
replaces the unprintable alert character with its printable
representation.
See also: do_string_escapes XREFdo_string_escapes.