Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:41:09+00:00 2026-06-15T11:41:09+00:00

I am trying to port some C Code but I am really stuck cause

  • 0

I am trying to port some C Code but I am really stuck cause of the use of memcpy I tried with ctypes (did not work). I am hoping to find a python way of using an equivalent function of memcpy

Any ideas

Here is an example of the C Code I am trying to port

i = l + 5;
t = htons(atoi(port));
memcpy((buf+i), &t, 2);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:41:10+00:00Added an answer on June 15, 2026 at 11:41 am

    You almost certainly don’t need to call htons and then copy the 2 bytes into a buffer—see Keith’s answer for why.

    However, if you do need to do this (maybe you’re crafting IP packets to compare to captured wire packets as a test or something?), you can.

    First, if you’re using a bytearray (or anything else that meets the writable buffer protocol), you just use normal list-style slice assignment:

    # like C's memcpy(buf+i, foo, 2)
    buf[i:i+2] = foo
    

    You don’t have that two-byte string foo; you have a short integer. In C, you can turn that into a pointer to two bytes just by using the & operator to get its address, but Python can’t do that. Fortunately, there’s a standard library module called struct designed for exactly this kind of thing:

    t = socket.htons(int(port))
    buf[i:i+2] = struct.pack('h', t)
    

    Or, because struct can handle endianness for you:

    t = int(port)
    buf[i:i+2] = struct.pack('!h', t)
    

    However, often you don’t even need the buffer copying; you can define the entire structure all at once inside struct. For example, if you’re trying to pack an IP address and port into a 6-byte array, you could do this:

    buf = bytearray(6)
    i = 0
    addrbytes = [int(part) for part in addr.split('.')]
    buf[i:i+4] = struct.pack('4B', addrbytes[0], addrbytes[1], addrbytes[2], addrbytes[3])
    i += 4
    portshort = int(port)
    buf[i:i+2] = struct.pack('!h', portshort)
    

    But this is much simpler:

    addrbytes = [int(part) for part in addr.split('.')]
    portshort = int(port)
    buf = struct.pack('!4Bh', addrbytes[0], addrbytes[1], addrbytes[2], addrbytes[3], portshort)
    

    I’ve just defined a structure that’s in network order, with four bytes followed by a short, and packed my data into it.

    One last thing to mention: If you really want to deal with C-style variables using C-style code, the ctypes module is another option. It’s made specifically for interacting with C code, so in general it’s pretty low-level (and the only module in the standard library that lets you segfault your code), but it let you build some nice mid-level stuff that looks a little more like C:

    class ADDRPORT(ctypes.BigEndianStructure):
        _fields_ = [("addr", ctypes.c_char*4),
                    ("port", ctypes.c_short)]
    
    addrport = ADDRPORT(addrbytes, portshort)
    

    Since your C code is progressively filling up a buffer, rather than setting elements of a struct, this probably isn’t what you want. But it’s worth being aware of, because it probably will be what you want at some point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to port some code to 64-bit, but it seems that the thread
I'm trying to port some code from Linux to Windows. I really don't know
I am trying to port some code I wrote in C# to Java, but
I am trying to port some code to use the mmap package. I am
I am trying to port some code form java do F# that generates a
I am trying to port some code using DevExpress XtraGrid to Wpf. What is
I'm trying to port some Windows code to Linux, ideally through platform-independent libraries (eg
I'm trying to port some of my c++ code into c. I have the
I am trying to port some code from a regular Java program into the
I'm trying to port some C++ code to C#, and one of the things

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.