Difference between revisions of "Byte"

From Free Pascal wiki
Jump to navigationJump to search
m (Reverted edits by Reverse22 (Talk); changed back to last version by Junior)
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 4: Line 4:
  
 
For example:
 
For example:
<delphi>
+
<syntaxhighlight>
 
var  
 
var  
 
   c: byte;  
 
   c: byte;  
Line 12: Line 12:
 
   c := 'A'; ch := 65;  { while they are the same action, this is illegal }
 
   c := 'A'; ch := 65;  { while they are the same action, this is illegal }
 
end.
 
end.
</delphi>
+
</syntaxhighlight>
  
 
The use of byte or char as a data type provides better documentation as to the purpose of the use of the particular variable.  The byte type can be [[coersion|coerced]] to char by using the '''[[chr]]''' function. Char type values can be coerced to byte by using the '''[[ord]]''' function
 
The use of byte or char as a data type provides better documentation as to the purpose of the use of the particular variable.  The byte type can be [[coersion|coerced]] to char by using the '''[[chr]]''' function. Char type values can be coerced to byte by using the '''[[ord]]''' function
Line 18: Line 18:
 
The above program corrected to legal use:
 
The above program corrected to legal use:
  
<delphi>
+
<syntaxhighlight>
 
var  
 
var  
 
   c: byte;  
 
   c: byte;  
Line 26: Line 26:
 
   c := ord('A'); ch := Chr(65); { now legal ... ch := Chr(65); is equivalent to ch := #65}
 
   c := ord('A'); ch := Chr(65); { now legal ... ch := Chr(65); is equivalent to ch := #65}
 
end.
 
end.
</delphi>
+
</syntaxhighlight>
  
 
{{Data types}}
 
{{Data types}}
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

Revision as of 14:56, 24 March 2012

Deutsch (de) English (en) español (es) suomi (fi) français (fr) italiano (it) русский (ru) 中文(中国大陆)‎ (zh_CN)

A byte is an unsigned integer in the range of 0 .. 255. A byte is 8 bits long. A byte and a char are the same thing, except a byte can only be referred to as a numeric type, while a char can be used as a character, or as part of a string type, and cannot be used in an arithmetic expression.

For example:

var 
  c: byte; 
  ch: char;
begin
  c := 65;  ch := 'A';  { are the same action, and are legal }
  c := 'A'; ch := 65;   { while they are the same action, this is illegal }
end.

The use of byte or char as a data type provides better documentation as to the purpose of the use of the particular variable. The byte type can be coerced to char by using the chr function. Char type values can be coerced to byte by using the ord function

The above program corrected to legal use:

var 
  c: byte; 
  ch: char;
begin
  c := 65;  ch := 'A'; { are the same action, and are legal }
  c := ord('A'); ch := Chr(65); { now legal ... ch := Chr(65); is equivalent to ch := #65}
end.


navigation bar: data types
simple data types

boolean byte cardinal char currency double dword extended int8 int16 int32 int64 integer longint real shortint single smallint pointer qword word

complex data types

array class object record set string shortstring