(octave.info)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: Note: dec2bin, Note: base2dec,
Note: hex2dec.
-- : 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: Note: bin2dec, Note: dec2base,
Note: dec2hex.
-- : 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: Note: hex2dec, Note: dec2base,
Note: dec2bin.
-- : 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: Note: dec2hex, Note: base2dec,
Note: bin2dec.
-- : 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: Note: base2dec, Note: dec2bin,
Note: dec2hex.
-- : 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: Note: dec2base, Note: bin2dec,
Note: hex2dec.
-- : S = num2hex (N)
-- : S = num2hex (N, "cell")
Convert a numeric array to an array of hexadecimal strings.
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"
With the optional second argument "cell", return a cell array of
strings instead of a character array.
See also: Note: hex2num, Note: hex2dec,
Note: dec2hex.
-- : N = hex2num (S)
-- : N = hex2num (S, CLASS)
Typecast a hexadecimal character array or cell array of strings to
an array of numbers.
By default, the input array is interpreted as a hexadecimal number
representing a double precision value. 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 second argument CLASS may be used to cause the input
array to be interpreted as a different value type. Possible values
are
Option Characters
----------------------
"int8" 2
"uint8" 2
"int16" 4
"uint16" 4
"int32" 8
"uint32" 8
"int64" 16
"uint64" 16
"char" 2
"single" 8
"double" 16
For example:
hex2num (["402df854"; "41200000"], "single")
⇒ [2.7183; 10.000]
See also: Note: num2hex, Note: hex2dec,
Note: dec2hex.
-- : 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: Note: str2num.
-- : 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: Note: deblank, Note: strrep,
Note: strtrim, Note: untabify.
-- : 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: Note: str2double, Note: eval.
-- : 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: Note: toupper.
-- : 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: Note: tolower.
-- : NATIVE_BYTES = unicode2native (UTF8_STR, CODEPAGE)
-- : NATIVE_BYTES = unicode2native (UTF8_STR)
Convert UTF-8 string UTF8_STR to byte stream using CODEPAGE.
The character vector UTF8_STR is converted to a byte stream
NATIVE_BYTES using the code page given by CODEPAGE. The string
CODEPAGE must be an identifier of a valid code page. Examples for
valid code pages are "ISO-8859-1", "Shift-JIS", or "UTF-16". For a
list of supported code pages, see
<https://www.gnu.org/software/libiconv>. If CODEPAGE is omitted or
empty, the system default codepage is used.
If any of the characters cannot be mapped into the codepage
CODEPAGE, they are replaced with the appropriate substitution
sequence for that codepage.
See also: Note: native2unicode.
-- : UTF8_STR = native2unicode (NATIVE_BYTES, CODEPAGE)
-- : UTF8_STR = native2unicode (NATIVE_BYTES)
Convert byte stream NATIVE_BYTES to UTF-8 using CODEPAGE.
The numbers in the vector NATIVE_BYTES are rounded and clipped to
integers between 0 and 255. This byte stream is then mapped into
the code page given by the string CODEPAGE and returned in the
string UTF8_STR. Octave uses UTF-8 as its internal encoding. The
string CODEPAGE must be an identifier of a valid code page.
Examples for valid code pages are "ISO-8859-1", "Shift-JIS", or
"UTF-16". For a list of supported code pages, see
<https://www.gnu.org/software/libiconv>. If CODEPAGE is omitted or
empty, the system default codepage is used.
If NATIVE_BYTES is a string vector, it is returned as is.
See also: Note: unicode2native.
-- : 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: Note: undo_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: Note: do_string_escapes.
automatically generated by info2www version 1.2.2.9