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);
You almost certainly don’t need to call
htonsand 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 normallist-style slice assignment: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 calledstructdesigned for exactly this kind of thing:Or, because
structcan handle endianness for you: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:But this is much simpler:
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:
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.