Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #46746

Re: How to get an integer from a sequence of bytes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(of': 0.07; 'assign': 0.07; 'element': 0.07; 'string': 0.09; 'conversions': 0.09; 'latter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; '(unsigned': 0.16; '1);': 0.16; '8-bit': 0.16; 'assigns': 0.16; 'byte,': 0.16; 'bytes;': 0.16; 'character).': 0.16; 'hexadecimal': 0.16; 'it;': 0.16; 'notation': 0.16; 'notation,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subscripting': 0.16; 'substring': 0.16; 'language': 0.16; 'code.': 0.18; 'code,': 0.22; 'example': 0.22; '2.x': 0.24; 'byte': 0.24; 'bytes': 0.24; 'char': 0.24; 'url:home': 0.24; '(or': 0.24; 'equivalent': 0.26; 'certain': 0.27; 'values': 0.27; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'character': 0.29; "doesn't": 0.30; 'compared': 0.30; '3.x': 0.31; 'asks': 0.31; 'decimal': 0.31; 'mod': 0.31; 'ok.': 0.31; 'languages': 0.32; 'stuff': 0.32; 'maybe': 0.34; 'subject:from': 0.34; 'equal': 0.35; 'but': 0.35; '+0200,': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'similar': 0.36; 'step': 0.37; 'to:addr:python-list': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'even': 0.60; 'length': 0.61; 'first': 0.61; "you've": 0.63; 'different': 0.65; 'note:': 0.66; 'direct': 0.67; 'subject:get': 0.81; 'whereas': 0.91; 'received:108': 0.93; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: How to get an integer from a sequence of bytes
Date Sun, 02 Jun 2013 21:48:36 -0400
Organization > Bestiaria Support Staff <
References <knvrhf$n5n$1@news.albasani.net> <mailman.2253.1369668622.3114.python-list@python.org> <ko85jp$vn3$1@news.albasani.net> <mailman.2447.1369941788.3114.python-list@python.org> <kog67o$pt2$1@news.albasani.net>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-108-79-220-205.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 3.3/32.846
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2571.1370224134.3114.python-list@python.org> (permalink)
Lines 124
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1370224134 news.xs4all.nl 15896 [2001:888:2000:d::a6]:46296
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:46746

Show key headers only | View raw


On Sun, 02 Jun 2013 21:25:45 +0200, Mok-Kong Shen
<mok-kong.shen@t-online.de> declaimed the following in
gmane.comp.python.general:


> 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.

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-05-27 16:45 +0200
  Re: How to get an integer from a sequence of bytes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-27 15:00 +0000
    RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-28 03:37 +0300
  Re: How to get an integer from a sequence of bytes Ned Batchelder <ned@nedbatchelder.com> - 2013-05-27 11:30 -0400
    Re: How to get an integer from a sequence of bytes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-28 00:31 +0000
      Re: How to get an integer from a sequence of bytes Dave Angel <davea@davea.name> - 2013-05-27 20:41 -0400
    Re: How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-05-30 20:26 +0200
      Re: How to get an integer from a sequence of bytes Ian Kelly <ian.g.kelly@gmail.com> - 2013-05-30 12:42 -0600
        Re: How to get an integer from a sequence of bytes jmfauth <wxjmfauth@gmail.com> - 2013-05-30 11:53 -0700
      Re: How to get an integer from a sequence of bytes Ned Batchelder <ned@nedbatchelder.com> - 2013-05-30 15:22 -0400
        Re: How to get an integer from a sequence of bytes Mok-Kong Shen <mok-kong.shen@t-online.de> - 2013-06-02 21:25 +0200
          Re: How to get an integer from a sequence of bytes Chris Angelico <rosuav@gmail.com> - 2013-06-03 05:54 +1000
          Re: How to get an integer from a sequence of bytes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-02 21:48 -0400
            Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-03 14:31 +0000
              Re: How to get an integer from a sequence of bytes Dave Angel <d@davea.name> - 2013-06-03 18:07 -0400
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-03 22:34 +0000
              Re: How to get an integer from a sequence of bytes Dan Stromberg <drsalists@gmail.com> - 2013-06-03 15:41 -0700
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-04 13:39 +0000
                Re: How to get an integer from a sequence of bytes Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-04 20:51 +0100
                Re: How to get an integer from a sequence of bytes Chris Angelico <rosuav@gmail.com> - 2013-06-05 07:49 +1000
                Re: How to get an integer from a sequence of bytes Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-04 20:34 -0400
                Re: How to get an integer from a sequence of bytes Tim Roberts <timr@probo.com> - 2013-06-04 22:11 -0700
                Re: How to get an integer from a sequence of bytes Fábio Santos <fabiosantosart@gmail.com> - 2013-06-12 15:00 +0100
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-12 14:42 +0000
              RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 02:18 +0300
                Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-06-04 13:42 +0000
                RE: How to get an integer from a sequence of bytes Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-06-04 16:58 +0300
              Re: How to get an integer from a sequence of bytes Dan Stromberg <drsalists@gmail.com> - 2013-06-03 16:47 -0700
  Re: How to get an integer from a sequence of bytes Grant Edwards <invalid@invalid.invalid> - 2013-05-28 16:04 +0000

csiph-web