Difference between revisions of "Byte"

From Lazarus wiki
Jump to navigationJump to search
m
m (Squash last deprecated enclosed attribute :-)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
{{Byte}}
 
{{Byte}}
  
A <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> is an unsigned [[Integer|<syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>]] in the range of <syntaxhighlight lang="pascal" enclose="none">0..255</syntaxhighlight>.
+
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" enclose="none">byte</syntaxhighlight> is 8&nbsp;bits long.
+
A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> is 8&nbsp;bits long.
A <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> and a [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>]] are virtually the same thing as of version&nbsp;3 of [[FPC]].
+
A <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight> and a [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]] are virtually the same thing as of version&nbsp;3 of [[FPC]].
  
== valid values ==
+
== Valid values ==
The key difference is, a <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> can only be referred to as a numeric [[Type|<syntaxhighlight lang="pascal" enclose="none">type</syntaxhighlight>]], while a <syntaxhighlight lang="pascal" enclose="none">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" enclose="none">byte</syntaxhighlight> will always be the same size as an [[AnsiChar|<syntaxhighlight lang="pascal" enclose="none">ansiChar</syntaxhighlight>]], but in the future <syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight> may be considered a synonym for [[WideChar|<syntaxhighlight lang="pascal" enclose="none">wideChar</syntaxhighlight>]], not <syntaxhighlight lang="pascal" enclose="none">ansiChar</syntaxhighlight>.
+
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:
 +
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
program byteDemo(input, output, stderr);
 
program byteDemo(input, output, stderr);
Line 29: Line 31:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The use of <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> or <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> as a data type provides better documentation as to the purpose of the use of the particular variable.
+
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 ==
+
== Standard functions ==
=== conversion to and from character ===
+
 
The <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight> type can be [[coersion|coerced]] to <syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight> by using the [[Chr|<syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight> function]].
+
=== Conversion to and from character ===
Char type values can be coerced to byte by using the [[Ord|<syntaxhighlight lang="pascal" enclose="none">ord</syntaxhighlight> function]].
+
 
 +
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">
 
<syntaxhighlight lang="pascal">
 
program ordChrDemo(input, output, stderr);
 
program ordChrDemo(input, output, stderr);
Line 61: Line 66:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== string representation ===
+
=== String representation ===
The {{Doc|package=RTL|unit=system|identifier=binstr|text=<syntaxhighlight lang="pascal" enclose="none">binStr</syntaxhighlight> function}} from the [[System unit|<syntaxhighlight lang="pascal" enclose="none">system</syntaxhighlight> unit]] can be used to get a [[String|<syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>]] showing the [[Binary numeral system|binary representation]] of a <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>:
+
 
 +
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">
 
<syntaxhighlight lang="pascal">
Line 75: Line 81:
 
end.
 
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
The output is:
 
The output is:
 +
 
<syntaxhighlight lang="text">
 
<syntaxhighlight lang="text">
 
00001010
 
00001010
 
</syntaxhighlight>
 
</syntaxhighlight>
A more versatile function is {{Doc|package=RTL|unit=strutils|identifier=inttobin|text=<syntaxhighlight lang="pascal" enclose="none">intToBin</syntaxhighlight> provided by the <syntaxhighlight lang="pascal" enclose="none">strUtils</syntaxhighlight> unit}}.
+
 
 +
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}}
 
{{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.



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