uint8

Converts values to unsigned 8-bit integers (0 to 255).

Category: Language Fundamentals > Data Types

Syntax

Y = uint8(X)

Converts X to class uint8.

Description

Values are rounded to the nearest integer (halves away from zero) and then saturate: anything above 255 becomes 255, and negative numbers and NaN become 0. No error or warning is raised. For example, uint8(300) gives 255.

Arithmetic between uint8 and a double gives a uint8 result, rounded and saturated again, so uint8(7)/uint8(2) is 4. Mixing two different integer classes in one operation is an error. intmax('uint8') gives the largest value. It is the usual class for image pixels.

Input arguments

X
Numeric, logical or char array.

Output arguments

Y
Array of class uint8 with the same size as X.

Example

Rounding and saturation

a = uint8(250);
a + 10
a - 255
uint8([1.4 1.5 2.5 -3])
uint8(7)/uint8(2)
Output
ans = uint8
   255
ans = uint8
   0
ans = 1×4 uint8 row vector

   1   2   3   0
ans = uint8
   4

See also

uint16 double intmax cast