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") ⇒ ans = 255
Return the decimal number corresponding to the binary number represented by the string s. For example:
bin2dec ("1110") ⇒ 14Spaces are ignored during conversion and may be used to make the binary number more readable.
bin2dec ("1000 0001") ⇒ 129If 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.
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.
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.
Return the integer corresponding to the hexadecimal number represented by the string s. For example:
hex2dec ("12B") ⇒ 299 hex2dec ("12b") ⇒ 299If 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.
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.
Convert s from a string of digits in base base to a decimal integer (base 10).
base2dec ("11120", 3) ⇒ 123If 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
Typecast a double precision number or vector to a 16 character hexadecimal string of the IEEE 754 representation of the number. For example:
num2hex ([-1, 1, e, Inf, NaN, NA]); ⇒ "bff0000000000000 3ff0000000000000 4005bf0a8b145769 7ff0000000000000 fff8000000000000 7ff00000000007a2"
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]
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
, andNA
are also accepted.s may also be a character matrix, in which case the conversion is repeated for each row. Or s may be a cell array of strings, in which case each element is converted and an array of the same dimensions is returned.
str2double
returns NaN for elements of s which cannot be converted.
str2double
can replacestr2num
, and it avoids the security risk of usingeval
on unknown data.See also: str2num.
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"
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 6The 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 theeval
function to do the conversion,str2num
will execute any code contained in the string s. Usestr2double
for a safer and faster conversion.For cell array of strings use
str2double
.See also: str2double, eval.
Return ASCII representation of s in a matrix. For example:
toascii ("ASCII") ⇒ [ 65, 83, 67, 73, 73 ]See also: char.
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.
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.
Convert special characters in string to their escaped forms.
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 = \areplaces the unprintable alert character with its printable representation.