Zeigt wie man einen Integer in ein char Array verpackt und diese wieder rückgängig macht
#include <iostream>
using namespace std;
void pack(unsigned char *o_pBuffer, int i_iNumber)
{
o_pBuffer[0] = i_iNumber>>24;
o_pBuffer[1] = i_iNumber>>16;
o_pBuffer[2] = i_iNumber>>8;
o_pBuffer[3] = i_iNumber;
}
int unpack(unsigned char *i_pBuffer)
{
return int(i_pBuffer[0])<<24 |
int(i_pBuffer[1])<<16 |
int(i_pBuffer[2])<<8 |
int(i_pBuffer[3]);
}
void main()
{
unsigned char buffer[4];
pack(buffer, -15254);
cout<<unpack(buffer)<<endl;
system("pause");
}
Kommentare zum Snippet