next up previous contents
Next: UINT16 Convert to Unsigned Up: Type Cast Functions Previous: Type Cast Functions   Contents

Subsections

UINT8 Convert to Unsigned 8-bit Integer

Usage

Converts the argument to an unsigned 8-bit Integer. The syntax for its use is

   y = uint8(x)

where x is an n-dimensional numerical array. Conversion follows the general C rules (e.g., if x is outside the normal range for an unsigned 8-bit integer of [0,255], the least significant 8 bits of x are used after conversion to an integer). Note that both NaN and Inf both map to 0.

Example

The following piece of code demonstrates several uses of uint8.

--> uint8(200)
ans = 
  <uint8>  - size: [1 1]
 200

In the next example, an integer outside the range of the type is passed in. The result is the 8 least significant bits of the argument.

--> uint8(400)
ans = 
  <uint8>  - size: [1 1]
 144

In the next example, a negative integer is passed in. The result is the 8 least significant bits of the argument, after taking the 2's complement.

--> uint8(-100)
ans = 
  <uint8>  - size: [1 1]
 156

In the next example, a positive double precision argument is passed in. The result is the unsigned integer that is closest to the argument.

--> uint8(pi)
ans = 
  <uint8>  - size: [1 1]
   3

In the next example, a complex argument is passed in. The result is the unsigned integer that is closest to the real part of the argument.

--> uint8(5+2*i)
ans = 
  <uint8>  - size: [1 1]
   5

In the next example, a string argument is passed in. The string argument is converted into an integer array corresponding to the ASCII values of each character.

--> uint8('helo')
ans = 
  <uint8>  - size: [1 4]
 
Columns 1 to 4
 104  101  108  111

In the last example, a cell-array is passed in. For cell-arrays and structure arrays, the result is an error.

--> uint8({4})
Error: Cannot convert cell-arrays to any other type.


next up previous contents
Next: UINT16 Convert to Unsigned Up: Type Cast Functions Previous: Type Cast Functions   Contents
Samit K. Basu 2005-03-16