1

I am required to transmit a BYTE array containing hexadecimal values of size no more than 250 bytes in the following format:

header|no of data structures|Data/Payload

Now, I have read multiple posts on stackoverflow regarding memory allocation to a an array but i didn't reach any viable solution. I am required to restrict the data to 250 bytes as i am going to be sending this data to a device that reads only 250 bytes.

Currently the size of my array according to python is 416 bytes.

enter image description here

3
  • Do you mean a list, array (from use array), a numpy array, or a bytearray? Commented Jun 8, 2016 at 7:08
  • bytearray to be precise. Commented Jun 8, 2016 at 7:15
  • Please edit your question to make that clear. Compression is data dependant. Have you looked at struct.pack()? Commented Jun 8, 2016 at 7:18

2 Answers 2

2

Declaring a byte array of size 250 makes a byte array that is equal to 250 bytes, however python's memory management is programmed in such a way that it acquires more space for an integer or a character as compared to C or other languages where you can assign an integer to be short or long. The sys.getsizeof() command ,as another user pointed out ,represents the internal representation of your data structure and not the actual size of the data when it is transmitted to another device.

data= bytearray(b'\x00'*length)

The size in bytes of the byte array is determined by the length. For example if one needs to send data of n bytes then length will be equal to n. I verified it by checking the size of the data received on the receiver's end.

Sign up to request clarification or add additional context in comments.

Comments

0

sys.getsizeof(newdata) is returning the internal representation of your data structure, and is system dependent. Refer to sys — System-specific parameters and functions

Try this link: How many bytes does a string have

1 Comment

Do you think that the size of data determined by the python script will affect the data being received by the device?. Is there a possibility that python is determining more bytes than required? and it wouldn't be an issue once the data is transmitted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.