meta data for this page
  •  

Packing data to character buffer

In previous years some people had problems with packing integers to character buffers so here is an example about how to do it. Just remember to keep track of the amount of bytes each integer reserves from the buffer used for storing data.

//PACK
*(uint16_t*)&buffer_holding_data[position_to_store] = htons(value_to_store);
 
//UNPACK
uint16_t retrieved_value = ntohs(*(uint16_t*)&buffer_holding_data[position_from_to_retrieve]);

CT30A5002 - Games and Networking