Difference between revisions of "Byte"
m (Squash last deprecated enclosed attribute :-) |
|||
(29 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
{{Byte}} | {{Byte}} | ||
− | A | + | A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> is an unsigned [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]] in the range of <syntaxhighlight lang="pascal" inline>0..255</syntaxhighlight>. |
+ | A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> is 8 bits long. | ||
+ | A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> and a [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]] are virtually the same thing as of version 3 of [[FPC]]. | ||
+ | |||
+ | == Valid values == | ||
+ | |||
+ | The key difference is, a <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> can only be referred to as a numeric [[Type|<syntaxhighlight lang="pascal" inline>type</syntaxhighlight>]], while a <syntaxhighlight lang="pascal" inline>char</syntaxhighlight> can be used as a character, or as part of a string type, and cannot be used in an arithmetic expression. | ||
+ | A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> will always be the same size as an [[AnsiChar|<syntaxhighlight lang="pascal" inline>ansiChar</syntaxhighlight>]], but in the future <syntaxhighlight lang="pascal" inline>char</syntaxhighlight> may be considered a synonym for [[WideChar|<syntaxhighlight lang="pascal" inline>wideChar</syntaxhighlight>]], not <syntaxhighlight lang="pascal" inline>ansiChar</syntaxhighlight>. | ||
For example: | For example: | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | The use of byte or | + | <syntaxhighlight lang="pascal"> |
+ | program byteDemo(input, output, stderr); | ||
+ | |||
+ | var | ||
+ | foo: byte; | ||
+ | bar: char; | ||
+ | |||
+ | begin | ||
+ | // those two assignments are physically the same | ||
+ | foo := 65; | ||
+ | bar := 'A'; | ||
+ | |||
+ | // although they are the same action, | ||
+ | // the following would be illegal | ||
+ | //foo := 'A'; | ||
+ | //bar := 65; | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | The use of <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> or <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> as a data type provides better documentation as to the purpose of the use of the particular variable. | ||
+ | |||
+ | == Standard functions == | ||
+ | |||
+ | === Conversion to and from character === | ||
+ | |||
+ | The <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> type can be [[coersion|coerced]] to <syntaxhighlight lang="pascal" inline>char</syntaxhighlight> by using the [[Chr|<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight> function]]. | ||
+ | Char type values can be coerced to byte by using the [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight> function]]. | ||
The above program corrected to legal use: | The above program corrected to legal use: | ||
− | < | + | <syntaxhighlight lang="pascal"> |
− | + | program ordChrDemo(input, output, stderr); | |
− | + | ||
− | + | var | |
− | + | foo: byte; | |
− | + | bar: char; | |
− | + | ||
− | : end. | + | begin |
− | </ | + | foo := 65; |
+ | bar := 'A'; | ||
+ | |||
+ | foo := ord('A'); | ||
+ | // chr(65) is equivalent to #65 | ||
+ | bar := chr(65); | ||
+ | bar := #65; | ||
+ | |||
+ | // alternatively: typecasts | ||
+ | // typecasts of constant expressions | ||
+ | // are guaranteed to happen at compile-time | ||
+ | foo := byte('A'); | ||
+ | bar := char(65); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
− | {{ | + | === String representation === |
+ | |||
+ | The {{Doc|package=RTL|unit=system|identifier=binstr|text=<syntaxhighlight lang="pascal" inline>binStr</syntaxhighlight> function}} from the [[System unit|<syntaxhighlight lang="pascal" inline>system</syntaxhighlight> unit]] can be used to get a [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]] showing the [[Binary numeral system|binary representation]] of a <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>: | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | program binStrDemo(input, output, stderr); | ||
+ | |||
+ | var | ||
+ | foo: byte; | ||
+ | |||
+ | begin | ||
+ | foo := 10; | ||
+ | writeLn(binStr(foo, 8)); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | The output is: | ||
+ | |||
+ | <syntaxhighlight lang="text"> | ||
+ | 00001010 | ||
+ | </syntaxhighlight> | ||
+ | A more versatile function is {{Doc|package=RTL|unit=strutils|identifier=inttobin|text=<syntaxhighlight lang="pascal" inline>intToBin</syntaxhighlight> provided by the <syntaxhighlight lang="pascal" inline>strUtils</syntaxhighlight> unit}}. | ||
− | + | {{Data types}} |
Latest revision as of 10:35, 26 May 2022
│
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 virtually the same thing as of version 3 of FPC.
Valid values
The key difference is, 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.
A byte
will always be the same size as an ansiChar
, but in the future char
may be considered a synonym for wideChar
, not ansiChar
.
For example:
program byteDemo(input, output, stderr);
var
foo: byte;
bar: char;
begin
// those two assignments are physically the same
foo := 65;
bar := 'A';
// although they are the same action,
// the following would be illegal
//foo := 'A';
//bar := 65;
end.
The use of byte
or byte
as a data type provides better documentation as to the purpose of the use of the particular variable.
Standard functions
Conversion to and from character
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:
program ordChrDemo(input, output, stderr);
var
foo: byte;
bar: char;
begin
foo := 65;
bar := 'A';
foo := ord('A');
// chr(65) is equivalent to #65
bar := chr(65);
bar := #65;
// alternatively: typecasts
// typecasts of constant expressions
// are guaranteed to happen at compile-time
foo := byte('A');
bar := char(65);
end.
String representation
The binStr
function from the system
unit can be used to get a string
showing the binary representation of a byte
:
program binStrDemo(input, output, stderr);
var
foo: byte;
begin
foo := 10;
writeLn(binStr(foo, 8));
end.
The output is:
00001010
A more versatile function is intToBin
provided by the strUtils
unit.
simple data types |
|
---|---|
complex data types |