0

Hey I have this code where I want to represent 1 as two bytes but when I access it back I am getting (1,) I do not want the comma and want to equate it like y==1

var = struct.pack(' h ', 1)
x = calcsize('h')
print(x)
y=struct.unpack('h', var)
print(y) 

2 Answers 2

1

struct.unpack always returns a tuple regardless of the number of elements. The easiest fix is to assign y like this:

y, = struct.unpack('h', var) # notice the comma

This will unpack the tuple into a single value

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

Comments

0

You can get the first value from the tuple like below,

y = struct.unpack('h', var)[0]

or you can get the first value like below,

y, *_ = struct.unpack('h', var)

or

y, = struct.unpack('h', var)

Comments

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.