Next: , Previous: Searching and Replacing, Up: Strings

### 5.3 String Conversions

— Function File: hex2dec (s)

Return the decimal number corresponding to the binary number stored in the string s. For example,

```          hex2dec ("1110")
=> 14
```

If s is a string matrix, returns a column vector of converted numbers, one per row of s. Invalid rows evaluate to NaN.

— Function File: dec2bin (n, len)

Return a binary number corresponding the nonnegative decimal number n, as a string of ones and zeros. For example,

```          dec2bin (14)
=> "1110"
```

If n is a vector, returns a string matrix, one row per value, 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.

— Function File: dec2hex (n, len)

Return the hexadecimal string corresponding to the nonnegative integer n. For example,

```          dec2hex (2748)
=> "ABC"
```

If n is a vector, returns a string matrix, one row per value, 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.

— Function File: hex2dec (s)

Returns the integer corresponding to the hexadecimal number stored in the string s. For example,

```          hex2dec ("12B")
=> 299
hex2dec ("12b")
=> 299
```

If s is a string matrix, returns a column vector of converted numbers, one per row of s. Invalid rows evaluate to NaN.

— Function File: dec2base (n, b, len)

Return a string of symbols in base b corresponding to the the nonnegative integer n.

```          dec2base (123, 3)
=> "11120"
```

If n is a vector, return a string matrix with one row per value, padded with leading zeros to the width of the largest value.

If b is a string then the characters of b are used as the symbols for the digits of n. 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.

— Function File: base2dec (s, b)

Convert s from a string of digits of base b into an integer.

```          base2dec ("11120", 3)
=> 123
```

If s is a matrix, returns a column vector with one value per row of s. If a row contains invalid symbols then the corresponding value will be NaN. Rows are right-justified before converting so that trailing spaces are ignored.

If b is a string, the characters of b are used as the symbols for the digits of s. Space (' ') may not be used as a symbol.

```          base2dec ("yyyzx", "xyz")
=> 123
```

— Function File: strjust (s, ["left"|"right"|"center"])

Shift the non-blank text of s to the left, right or center of the string. If s is a string array, justify each string in the array. Null characters are replaced by blanks. If no justification is specified, then all rows are right-justified.

— Function File: str2num (s)

Convert the string s to a number.

— Mapping Function: toascii (s)

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

```          toascii ("ASCII")
=> [ 65, 83, 67, 73, 73 ]

```

— Mapping Function: tolower (s)

Return a copy of the string s, with each upper-case character replaced by the corresponding lower-case one; nonalphabetic characters are left unchanged. For example,

```          tolower ("MiXeD cAsE 123")
=> "mixed case 123"
```

— Built-in Function: toupper (s)

Return a copy of the string s, with each lower-case character replaced by the corresponding upper-case one; nonalphabetic characters are left unchanged. For example,

```          toupper ("MiXeD cAsE 123")
=> "MIXED CASE 123"
```

— Built-in Function: do_string_escapes (string)

Convert special characters in string to their escaped forms.

— Built-in Function: undo_string_escapes (s)

Converts 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.

— Built-in Variable: warn_num_to_str

If the value of `warn_num_to_str` is nonzero, a warning is printed for implicit conversions of numbers to their ASCII character equivalents when strings are constructed using a mixture of strings and numbers in matrix notation. For example,

```          [ "f", 111, 111 ]
=> "foo"
```

elicits a warning if `warn_num_to_str` is nonzero. The default value is 1.

— Built-in Variable: warn_str_to_num

If the value of `warn_str_to_num` is nonzero, a warning is printed for implicit conversions of strings to their numeric ASCII equivalents. For example,

```          "abc" + 0
=> 97 98 99
```

elicits a warning if `warn_str_to_num` is nonzero. The default value is 0.

— Built-in Variable: warn_single_quote_string

Print warning if a signle quote character is used to introduce a string constant.