How to pack a character and a number correctly?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How do I determine the size of an object in Python?How do I sort a dictionary by value?How do I get the number of elements in a list in Python?In Python, how do I determine if an object is iterable?How do I list all files of a directory?How do I convert a String to an int in Java?
Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?
Are there any established rules for splitting books into parts, chapters, sections etc?
What are the components of a legend (in the sense of a tale, not a figure legend)?
declared variable inside void setup is forgotten in void loop
Is taking modulus on both sides of an equation valid?
When a land becomes a creature, is it untapped?
What is the limit on how high you can fly up?
If current results hold, Man City would win PL title
Smallest Guaranteed hash collision cycle length
Why do the lights go out when someone enters the dining room on this ship?
Why does the headset man not get on the tractor?
Tikz draw contour without some edges, and fill
On studying Computer Science vs. Software Engineering to become a proficient coder
Is the expression "To think you would stoop so low" often misused?
What episode was being referenced by this part of Discovery's season 2 episode 13 recap?
Why did I need to *reboot* to change my group membership
Program which behaves differently in/out of a debugger
German characters on US-International keyboard layout
correct spelling of "carruffel" (fuzz, hustle, all that jazz)
Conditional probability - sum of dice is even given that at least one is a five
What information do scammers need to withdraw money from an account?
Is Germany still exporting arms to countries involved in Yemen?
What to do if SUS scores contradict qualitative feedback?
Do I need to say 'o`clock'?
How to pack a character and a number correctly?
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory in Python?How do I determine the size of an object in Python?How do I sort a dictionary by value?How do I get the number of elements in a list in Python?In Python, how do I determine if an object is iterable?How do I list all files of a directory?How do I convert a String to an int in Java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm learning about client-server communication in python, and I want to send some packed structures.I want to pack a mathematical sign and a number. I tried like this:
idx = 50
value1 = "<"
value2 = idx
packer = struct.Struct('1s I')
packed_data = packer.pack(*value1, *value2)
But I got the error:
packed_data = packer.pack(*value1, *value2)
TypeError: 'int' object is not iterable
or this error:
packed_data = packer.pack(*value1, *value2)
struct.error: argument for 's' must be a bytes object
If I try like this:
value2 = [idx]
I don't know how to do this correctly.
python python-3.x int iterable pack
add a comment |
I'm learning about client-server communication in python, and I want to send some packed structures.I want to pack a mathematical sign and a number. I tried like this:
idx = 50
value1 = "<"
value2 = idx
packer = struct.Struct('1s I')
packed_data = packer.pack(*value1, *value2)
But I got the error:
packed_data = packer.pack(*value1, *value2)
TypeError: 'int' object is not iterable
or this error:
packed_data = packer.pack(*value1, *value2)
struct.error: argument for 's' must be a bytes object
If I try like this:
value2 = [idx]
I don't know how to do this correctly.
python python-3.x int iterable pack
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
You don't need the*
; it's justpacker.pack(value1, value2)
.
– chepner
Mar 23 at 13:19
@chepner also thestruct.error: argument for 's' must be a bytes object
error
– Gábor
Mar 23 at 13:23
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24
add a comment |
I'm learning about client-server communication in python, and I want to send some packed structures.I want to pack a mathematical sign and a number. I tried like this:
idx = 50
value1 = "<"
value2 = idx
packer = struct.Struct('1s I')
packed_data = packer.pack(*value1, *value2)
But I got the error:
packed_data = packer.pack(*value1, *value2)
TypeError: 'int' object is not iterable
or this error:
packed_data = packer.pack(*value1, *value2)
struct.error: argument for 's' must be a bytes object
If I try like this:
value2 = [idx]
I don't know how to do this correctly.
python python-3.x int iterable pack
I'm learning about client-server communication in python, and I want to send some packed structures.I want to pack a mathematical sign and a number. I tried like this:
idx = 50
value1 = "<"
value2 = idx
packer = struct.Struct('1s I')
packed_data = packer.pack(*value1, *value2)
But I got the error:
packed_data = packer.pack(*value1, *value2)
TypeError: 'int' object is not iterable
or this error:
packed_data = packer.pack(*value1, *value2)
struct.error: argument for 's' must be a bytes object
If I try like this:
value2 = [idx]
I don't know how to do this correctly.
python python-3.x int iterable pack
python python-3.x int iterable pack
edited Mar 23 at 13:12
Gábor
asked Mar 23 at 12:58
GáborGábor
876
876
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
You don't need the*
; it's justpacker.pack(value1, value2)
.
– chepner
Mar 23 at 13:19
@chepner also thestruct.error: argument for 's' must be a bytes object
error
– Gábor
Mar 23 at 13:23
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24
add a comment |
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
You don't need the*
; it's justpacker.pack(value1, value2)
.
– chepner
Mar 23 at 13:19
@chepner also thestruct.error: argument for 's' must be a bytes object
error
– Gábor
Mar 23 at 13:23
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
You don't need the
*
; it's just packer.pack(value1, value2)
.– chepner
Mar 23 at 13:19
You don't need the
*
; it's just packer.pack(value1, value2)
.– chepner
Mar 23 at 13:19
@chepner also the
struct.error: argument for 's' must be a bytes object
error– Gábor
Mar 23 at 13:23
@chepner also the
struct.error: argument for 's' must be a bytes object
error– Gábor
Mar 23 at 13:23
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24
add a comment |
1 Answer
1
active
oldest
votes
The first problem is that you are unnecessarily trying to (sequence-)unpack your arguments. The Struct
format expects a bytes
and an int
, and you (almost) already have them.
The second problem is that "<"
is a Unicode string, and pack
expects bytes instead. You need to properly encode the string first.
packed_data = packer.pack(value1.encode('utf-8'), value2)
The particular encoding you use doesn't matter, as long as you use the same one to unpack the data.
Note that if you did have a Unicode character that couldn't be encoded in one byte, your string format would be wrong. The struct
module doesn't handle variable-length strings by itself, so it would probably be simpler to just encode the int
by itself and concatenated that with your encoded string.
value =
packed_data = value1.encode('utf-8') + struct.pack("I", value2)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313957%2fhow-to-pack-a-character-and-a-number-correctly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The first problem is that you are unnecessarily trying to (sequence-)unpack your arguments. The Struct
format expects a bytes
and an int
, and you (almost) already have them.
The second problem is that "<"
is a Unicode string, and pack
expects bytes instead. You need to properly encode the string first.
packed_data = packer.pack(value1.encode('utf-8'), value2)
The particular encoding you use doesn't matter, as long as you use the same one to unpack the data.
Note that if you did have a Unicode character that couldn't be encoded in one byte, your string format would be wrong. The struct
module doesn't handle variable-length strings by itself, so it would probably be simpler to just encode the int
by itself and concatenated that with your encoded string.
value =
packed_data = value1.encode('utf-8') + struct.pack("I", value2)
add a comment |
The first problem is that you are unnecessarily trying to (sequence-)unpack your arguments. The Struct
format expects a bytes
and an int
, and you (almost) already have them.
The second problem is that "<"
is a Unicode string, and pack
expects bytes instead. You need to properly encode the string first.
packed_data = packer.pack(value1.encode('utf-8'), value2)
The particular encoding you use doesn't matter, as long as you use the same one to unpack the data.
Note that if you did have a Unicode character that couldn't be encoded in one byte, your string format would be wrong. The struct
module doesn't handle variable-length strings by itself, so it would probably be simpler to just encode the int
by itself and concatenated that with your encoded string.
value =
packed_data = value1.encode('utf-8') + struct.pack("I", value2)
add a comment |
The first problem is that you are unnecessarily trying to (sequence-)unpack your arguments. The Struct
format expects a bytes
and an int
, and you (almost) already have them.
The second problem is that "<"
is a Unicode string, and pack
expects bytes instead. You need to properly encode the string first.
packed_data = packer.pack(value1.encode('utf-8'), value2)
The particular encoding you use doesn't matter, as long as you use the same one to unpack the data.
Note that if you did have a Unicode character that couldn't be encoded in one byte, your string format would be wrong. The struct
module doesn't handle variable-length strings by itself, so it would probably be simpler to just encode the int
by itself and concatenated that with your encoded string.
value =
packed_data = value1.encode('utf-8') + struct.pack("I", value2)
The first problem is that you are unnecessarily trying to (sequence-)unpack your arguments. The Struct
format expects a bytes
and an int
, and you (almost) already have them.
The second problem is that "<"
is a Unicode string, and pack
expects bytes instead. You need to properly encode the string first.
packed_data = packer.pack(value1.encode('utf-8'), value2)
The particular encoding you use doesn't matter, as long as you use the same one to unpack the data.
Note that if you did have a Unicode character that couldn't be encoded in one byte, your string format would be wrong. The struct
module doesn't handle variable-length strings by itself, so it would probably be simpler to just encode the int
by itself and concatenated that with your encoded string.
value =
packed_data = value1.encode('utf-8') + struct.pack("I", value2)
edited Mar 23 at 13:30
answered Mar 23 at 13:22
chepnerchepner
268k38258351
268k38258351
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55313957%2fhow-to-pack-a-character-and-a-number-correctly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why is the packing necessary? I think what you need is serialization, not packing. have a look at JSON.dumps()
– g_uint
Mar 23 at 13:17
You don't need the
*
; it's justpacker.pack(value1, value2)
.– chepner
Mar 23 at 13:19
@chepner also the
struct.error: argument for 's' must be a bytes object
error– Gábor
Mar 23 at 13:23
Yeah, that's why I posted an answer instead of voting to close as "could no longer be reproduced".
– chepner
Mar 23 at 13:24