Converting 4 bytes back to an int. 
Author Message
 Converting 4 bytes back to an int.

Hi there all,
I cant figure out how to do the above.

I read from a file 4 bytes which represents an integer, which is
returned
to me as a string.
ie numberString = f.read( 4 )

How can I convert "numberString" back to an int?
Or do I need to to keep shifting it 4 times?
Anybody got any sample code?

Many thanks in advance,
Chai

--

|  "Poetry, like chastity, can be carried to far." -- Mark Twain



Wed, 21 May 2003 09:02:27 GMT  
 Converting 4 bytes back to an int.

Quote:

> I read from a file 4 bytes which represents an integer, which is
> returned
> to me as a string.
> ie numberString = f.read( 4 )

> How can I convert "numberString" back to an int?

assuming little-endian, since you're posting from
a windows box:

    import struct
    i = struct.unpack("<i", f.read(4))

for more info, see:
http://www.python.org/doc/lib/module-struct.html

</F>



Wed, 21 May 2003 09:10:41 GMT  
 Converting 4 bytes back to an int.


Quote:
>Hi there all,
>I cant figure out how to do the above.

>I read from a file 4 bytes which represents an integer, which is
>returned
>to me as a string.
>ie numberString = f.read( 4 )

>How can I convert "numberString" back to an int?
>Or do I need to to keep shifting it 4 times?
>Anybody got any sample code?

check out the struct module in the library documentation

hs



Wed, 21 May 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Converting python object reference to int and back

2. Converting 2 bytes string into INT

3. Converting big-endian byte to small-endian byte

4. Converting big-endian byte to small-endian byte

5. Need to unread or push back bytes to a file

6. What is the byte-compiler int Tcl/Tk 8.0

7. Converting images back to files

8. Converting back to Clarion 5 -Unresolved Error

9. Converting back to Clarion 5 - Unresolved Error

10. Convert from Clarion to DBase and back again

11. Wanted: A program that converts executables back to clipper sourcecode

12. Convert load module back to source code

 

 
Powered by phpBB® Forum Software