473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Encode Bytes

Hi All,
I am new to python and I am using a strip down version of
python that does not support struc,pack,etc.

I have a binary protocol that is define as follows:

PART OffSet Lenght
============================
ID 0 2
VER 2 1
CMD 3 2
PGKID 5 2
DATE 7 3
TIME 10 3
CRC 13 2
DLEN 15 2

How do I encode these the offset is in Bytes.

Thank you very much.

Jul 11 '07 #1
1 2224
On Jul 12, 4:40 am, "albert_k_arhin" <albert_k_ar...@yahoo.comwrote:
Hi All,
I am new to python and I am using a strip down version of
python that does not support struc,pack,etc.

I have a binary protocol that is define as follows:

PART OffSet Lenght
============================
ID 0 2
VER 2 1
CMD 3 2
PGKID 5 2
DATE 7 3
TIME 10 3
CRC 13 2
DLEN 15 2

How do I encode these the offset is in Bytes.
You haven't defined your data precisely enough to allow use of
struct.pack even if it were available to you.

Are all of the 1-byte fields unsigned integers (range 0 to 255
inclusive), or are some signed (range -128 to 127)?
Are all of the 2-byte fields unsigned integers (range 0 to 65535
inclusive) or are some signed (-32768 to 32767)?
What is the format of the 3-byte DATE and TIME fields? What type is
your starting value -- datetime.datetime?
Littlendian or bigendian?

Here's an example of the sort of function you'll need to write:
>>def packu2le(anint):
.... """Produce a 2-byte littlendian string from an unsigned
integer"""
.... assert 0 <= anint <= 65535
.... byte0 = anint & 0xff
.... byte1 = anint >8
.... return chr(byte0) + chr(byte1)
....
>>packu2le(0)
'\x00\x00'
>>packu2le(255)
'\xff\x00'
>>packu2le(256)
'\x00\x01'
>>packu2le(65535)
'\xff\xff'
>>packu2le(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in packu2le
AssertionError
>>>
HTH,
John

Jul 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ramy076 | last post by:
I have a C App which uses openssl for Encoding and decoding which needs to be ported to java. The decoded text does not match between the C App and the java App. The size of the output itself is...
5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
7
by: Richard Trahan | last post by:
I need a javascript function to hex-encode a plus sign so I can pass the plus sign as an argument in a GET request. escape() and encodeURI() don't do it (and probably shouldn't, because '+' is a...
1
by: Damir Hakimov | last post by:
Hi *! I found a strange bug in base64.encode and decode, when I try to encode - decode a file 1728512 bytes lenth. Is somebody meet with this? I don't attach the file because it big, but can...
1
by: Philip Wagenaar | last post by:
I want to mime encode a string. I think that mime is base64 coding (is this true?). I created the following code: Dim oEncoder As New System.Text.ASCIIEncoding Dim Str As String = "SYSTEM"...
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
6
by: 7stud | last post by:
s1 = "hello" s2 = s1.encode("utf-8") s1 = "an accented 'e': \xc3\xa9" s2 = s1.encode("utf-8") The last line produces the error: --- Traceback (most recent call last):
8
by: mario | last post by:
I have checks in code, to ensure a decode/encode cycle returns the original string. Given no UnicodeErrors, are there any cases for the following not to be True? unicode(s, enc).encode(enc)...
13
by: mario | last post by:
Hello! i stumbled on this situation, that is if I decode some string, below just the empty string, using the mcbs encoding, it succeeds, but if I try to encode it back with the same encoding it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.