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
Function File: 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.

Function File: 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.

Function File: 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.

Function File: 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.

Function File: dec2base (d, base)
Function File: 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.

Function File: 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.

Built-in Function: 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, hex2dec, dec2hex.

Built-in Function: n = hex2num (s)
Built-in Function: 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, hex2dec, dec2hex.

Built-in Function: 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.

Function File: strjust (s)
Function File: 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.

Function File: x = str2num (s)
Function File: [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.

Mapping Function: toascii (s)

Return ASCII representation of s in a matrix. For example:

toascii ("ASCII")
     ⇒ [ 65, 83, 67, 73, 73 ]

See also: char.

Mapping Function: tolower (s)
Mapping Function: 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.

Mapping Function: toupper (s)
Mapping Function: 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.

Built-in Function: do_string_escapes (string)

Convert special characters in string to their escaped forms.

Built-in Function: 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.


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