Skip to content

Instantly share code, notes, and snippets.

@brun0xff
Created May 12, 2017 23:07
Show Gist options
  • Save brun0xff/3793a16514240aaa05a78791109b2c65 to your computer and use it in GitHub Desktop.
Save brun0xff/3793a16514240aaa05a78791109b2c65 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void setUnsignedShortIntToChar(char * mm, unsigned short n)
{
/*
Pega 8 bits menos significativos de n e coloca nos menos
significativos de low8bits preenchendo os mais significativos
de low8bits com 0000 0000
*/
unsigned short low8bits = n & 0xFF;
mm[0] = n & 0xFF;
printf("Low: %d\n", low8bits);
unsigned short high8bits = (n >> 8) & 0xFF;
mm[1] = (n >> 8) & 0xFF;
printf("High: %d\n", high8bits);
}
void getCharToUnsignedShortInt(char * mm)
{
/*
Pega os 8 bits mais significativos de n e` coloca nos menos
significativos de high8bits preenchendo os mais significativos
de high8bits com 0000 0000
*/
unsigned short * num = (unsigned short *)mm;
printf("Dec get char: %d\n", *num);
}
int main(int argc, char const *argv[])
{
unsigned short n = 1924;
char * mm = (char *)malloc(sizeof(char)*5);
setUnsignedShortIntToChar(mm, n);
getCharToUnsignedShortInt(mm);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment