Re: Shift Confusion

2005-02-25 Thread Lars
Hi Kamilche,

Aside from the 7bit confusion you should take a look at the 'struct'
module. I bet it will simplify your life considerably.

#two chars
 import struct
 struct.pack('cc','A','B')
'AB'

#unsigned short + two chars
 struct.pack('Hcc',65535,'a','b')
'\xff\xffab'


Cheers
Lars

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread John Machin
On 23 Feb 2005 22:06:54 -0800, Kamilche [EMAIL PROTECTED]
wrote:

I'm trying to pack two characters into a single byte, and the shifting
in Python has me confused.

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.

It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.

To hold one ASCII character 0 = ord(c)  128, you need log2(128) == 7
bits. There are 8 bits in a byte. Therefore you can hold only 8/7.0 ==
1.14... ASCII characters in a standard 8-bit byte. If there is such a
thing as a 14-bit byte, that's news to me.

Other things you are doing wrong:

1. Using L.lower() as a variable name. Some fonts make it extremely
hard to work out what is what in l1l1l1l1l1l1ll1l1l -- can you read
that???

2. Hmmm:
  chr((y  1) | x)).upper()
'CHR((Y  1) | X))'

OK so that's a one, not the length of your string, as augmented.
Either would be be wrong. Shifting a character left ONE bit (or,
changing the emphasis, one BIT) and then ORing in another character
would be vaguely reasonable only if you were writing a hash function.

3. Supposing this modern alchemy had worked: when unpacking, how would
you know whether the string was originally (say) seven characters long
or 8 characters long?

4. Your unpacking routine appears to be trying to unpack two 4-bit
items (nibbles) out of a byte, but is not doing (temp  0xf0)  4 for
the top nibble as one might expect . aaahhh!!?? are you trying to
emulate packed decimal???

5. Not writing down a clear statement of what you are trying to do,
followed by examples of input and expected output. This latter goes by
fancy names like test-driven development; when I started programming
it was known as common sense.

6. Not using Python interactively to explore what's going on:

 ord('x')
120
 ord('y')
121
 (120  1)
240
 (120  1) | 121
249


HTH,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread qwweeeit
At programming level it seems correct (a part a return closure
needed for the main function).

But the error is IMHO conceptual:
for a char you need 7 bits (from 0 to 127 or in hex from x00 to x7F)
and you can't accomodate the other char in only one bit!
The other 128 symbols (from 128 to 255 or in hex from x80 to xFF) are
only possible because you use again 7 bits, but with the 8th bit set
to 1!

What you are trying to do I made in C language (some years ago...)
using however bytes and words, packing 2 bytes in only one word, but
you can't pack 2 chars (each one beeing nearly a byte) in a byte!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread Richard Brodie

John Machin [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]

 Essentially, it should be possible to use a 'packed string' format in
 Python, where as long as the characters you're sending are in the ASCII
 range 0 to 127, two will fit in a byte.

 It should be possible, but only in a realm where phlogiston and
 perpetual motion machines exist.

alt.sys.pdp10 ?


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread James Kew
Dennis Lee Bieber [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On 23 Feb 2005 22:06:54 -0800, Kamilche [EMAIL PROTECTED]
 declaimed the following in comp.lang.python:


 Essentially, it should be possible to use a 'packed string' format in
 Python, where as long as the characters you're sending are in the ASCII
 range 0 to 127, two will fit in a byte.

 Pardon? You are going to fit TWO 7-bit values into one 8-bit?

Quite. Although you can sort of see how one might naively arrive at this 
conclusion: one 7-bit char takes 0...127, which when you put it into an 
8-bit byte leaves 128...255 unused for a second char

James


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread Steve Holden
Dennis Lee Bieber wrote:
On Thu, 24 Feb 2005 14:22:59 -, Richard Brodie [EMAIL PROTECTED]
declaimed the following in comp.lang.python:

John Machin [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]

Essentially, it should be possible to use a 'packed string' format in
Python, where as long as the characters you're sending are in the ASCII
range 0 to 127, two will fit in a byte.
It should be possible, but only in a realm where phlogiston and
perpetual motion machines exist.
alt.sys.pdp10 ?
Closest thing I know of to what is being attempted is DEC's
RAD-50; but that was essentially just uppercase A..Z, 0..9, and a few
punctuation marks, and packed three of them into two bytes.
Another code it used was known as SIXBIT, allowing 64 different 
characters. IIRC it could cope with letters, digits and a bunch of 
punctuation - see

  http://nemesis.lonestar.org/reference/telecom/codes/sixbit.html
The DECSystem-10 used a 3-6 bit word, so you could get six sixbit 
characters to a word. In ASCII you could only get four (or, if you threw 
the parity bit away, five) characters to a word.

While its character-handling instructions weren't, as I recall, unique 
in the industry, the DECSystem-10 remains the only hardware I ever got 
to use that had instructions to handle variable byte sizes.

regards
 Steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: Shift Confusion

2005-02-24 Thread Kamilche
 Quite. Although you can sort of see how one might naively arrive at
this
 conclusion: one 7-bit char takes 0...127, which when you put it into
an
 8-bit byte leaves 128...255 unused for a second char

 James

Yep, that's what I was doing. Guess I was too tired to program usefully
last night. 

Thanks for clearing that up, guys!

-- 
http://mail.python.org/mailman/listinfo/python-list