On Fri, Dec 4, 2009 at 6:39 PM, mudit tuli <span dir="ltr"><<a href="mailto:mudit.tuli@gmail.com">mudit.tuli@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

I am very new to Python and started getting to know socket programming recently.<br>Made a socket server, which receives a "Single Octet"(treated as a single 8-bit integer field) from a client.<br>But I am not sure what to do with this "Single Octet" and how to decode it into a long integer, so that I can make use of it .<br>


Any Ideas ?<br>
<br></blockquote><div><br></div><div>Check out the "struct" module for low-level byte-stream protocols.</div><div><br></div><div>>>> my_byte = '\x0c'</div><div>>>> print struct.unpack("<B", my_byte)</div>

<div>(12, )</div><div><br></div><div>That would convert the byte string "my_byte" containing a single byte into a tuple according to the format string passed.. In this case, < specifies network/big-endian byte order, and "B" specifies that the the message contains a single unsigned byte as a number. The tuple will thus contain a 12.</div>

<div><br></div><div>There's some other more direct ways you can approach the problem, but struct is really IMHO best and using it early in your protocol is the best practice.</div><div><br></div></div><div name="mailplane_signature">

--S</div>