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;








0















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.










share|improve this question
























  • 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











  • @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


















0















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.










share|improve this question
























  • 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











  • @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














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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











  • 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











  • 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













1 Answer
1






active

oldest

votes


















0














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)





share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer





























      0














      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)





      share|improve this answer



























        0












        0








        0







        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)





        share|improve this answer















        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)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 13:30

























        answered Mar 23 at 13:22









        chepnerchepner

        268k38258351




        268k38258351





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현