dimanche 24 février 2019

How to copy int to u_char*

I have a u_char* dynamic array having binary data of some network packet. I want to change the destination port number in the packet with some integer value. Suppose that the port number offset within the packet is ofs, with length of 4 bytes.

I tried the following 2 methods:

u_char* packet = new u_char[packet_size]; // Packet still empty

// Read packet from network ...

int new_port = 1234;

Method #1:

std::copy((u_char*)&new_port, (u_char*)&new_port+4, packet+ofs);

Method #2:

std::string new_port_str = std::to_string(new_port);
auto new_port_bytes = new_port_str.c_str();
std::copy(new_port_bytes, new_port_bytes+4, packet+ofs);

Both methods give garbage value for port number (but the rest of the packet is OK). Could anyone help me ?

Aucun commentaire:

Enregistrer un commentaire