b'7' is the byte with the character 7 in a certain code, so that's
ok. In other PLs one assigns an int to a byte, with that int in either
In other languages "byte" is an 8-bit signed/unsigned numeric.
But what you have is a Python 3.x "bytes" structure -- similar to a
character string in Python 2.x...
decimal notation or hexadecimal notation, or else one assigns a
character to it, in which case it gets the value of the character
in a certain code. What I don't yet understand is why Python is
apprently different from other PLs in that point in not allowing direct
coersion of a byte to an int.
As you've been shown, the first step is that you may have to
subscript it; even with just one byte, the structure is still a
"string/array". NOTE: that example doesn't work in 2.7, since
subscripting what is a "string" still returns a substring (of one
character).
Python doesn't have a "numeric" byte type -- the b"..." is an
"array" of 8-bit values in Python 3.x, and is just a character string in
2.x
A language like C didn't have a "string" type... "char" was a
pseudonym for "numeric byte" (and some even support "unsigned char" vs
"signed char").
Maybe you'd like to program in Ada... Where "7" is a "string of
length 1" and '7' is a character -- and you have to do type conversions
to assign the latter to the former.
Heck:
with Text_IO; use Text_IO;
procedure Bytes is
begin
if "7" = '7' then
Put_Line ("string 7 is equal to character 7");
else
Put_Line ("string 7 is NOT equal to character 7");
end if;
end Bytes;
WON'T compile... string can not be compared to character!
with Text_IO; use Text_IO;
procedure Bytes is
A_String : String (1 .. 1);
A_Char : Character := '7';
begin
A_String := A_Char;
end Bytes;
The above fails to compile, whereas the following is valid Ada
with Text_IO; use Text_IO;
procedure Bytes is
A_String : String (1 .. 1);
A_Char : Character := '7';
begin
A_String(1) := A_Char;
end Bytes;
Don't even ask about /numeric/ bytes and strings (or characters). Or
lets...
with Text_IO; use Text_IO;
procedure Bytes is
type Byte is mod 256;
A_String : String (1 .. 1);
Char : Byte := 7;
begin
A_String (1) := Char;
end Bytes;
Fails... But...
with Text_IO; use Text_IO;
procedure Bytes is
type Byte is mod 256;
A_String : String (1 .. 1);
Char : Byte := 7;
begin
A_String (1) := Character'Val (Char);
end Bytes;
That takes a byte data type (unsigned 8-bit value)... Asks for the
CHARACTER data type having the value equivalent to the "position" of the
byte... And then stuff that into the only element of a STRING data type.