Created
May 12, 2017 23:07
-
-
Save brun0xff/3793a16514240aaa05a78791109b2c65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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