Next: , Previous: , Up: Strings   [Contents][Index]


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, base2dec, 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: bin2dec, dec2base, 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: hex2dec, dec2base, 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: dec2hex, base2dec, 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: base2dec, dec2bin, 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: dec2base, bin2dec, 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: hex2num, hex2dec, 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

OptionCharacters
"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: num2hex, hex2dec, 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':

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.

: 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, strrep, strtrim, 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: str2double, 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: 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: 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: 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: 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: 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: do_string_escapes.


Next: , Previous: , Up: Strings   [Contents][Index]