Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute C Online

#include <stdio.h>

union d
{
   unsigned short sh;
   unsigned char ch[2];
}hextoi;


union e
{
   unsigned int sh;
   unsigned char ch[4];
}hextobi;

void conversion( unsigned int *ptr_int)
{
    //unsigned short tmp=*((unsigned short *)(ptr_int)+1);
   // tmp= ((tmp<<8)&0xff00)|((tmp>>8)&0x00ff);
    
  *((unsigned short *)ptr_int)= ((*((unsigned short *)ptr_int)<<8)&0xff00)|((*((unsigned short *)ptr_int)>>8)&0x00ff);
  
  *((unsigned short *)(ptr_int)+1) = ((*((unsigned short *)(ptr_int)+1)<<8)&0xff00)|((*((unsigned short *)(ptr_int)+1)>>8)&0x00ff);
  
    
    //printf("Temp: %.2X\n",tmp);
    
}

int main()
{
    //unsigned char a[2]={0x01,0x02};
    hextoi.ch[0]=0x18;
    hextoi.ch[1]=0x98;
    
    hextobi.ch[0]=0x03;
    hextobi.ch[1]=0xE8;
    hextobi.ch[2]=0x01;
    hextobi.ch[3]=0x12;
    
    printf("Data1 -> %.2X %.2X , int -> %.2X\n", hextoi.ch[0],hextoi.ch[1], hextoi.sh);
    
    conversion(&hextobi.sh);
    
    printf("Data2 -> %.2X %.2X %.2X %.2X , int -> %.2X\n", hextobi.ch[0],hextobi.ch[1],hextobi.ch[2],hextobi.ch[3], hextobi.sh);
    
     
    
    //unsigned short a1= (hextobi.sh>>16);
   
    // printf("a1 %.2X\n",a1);
    
    //hextoi.sh = (hextoi.sh<<8)&0xff00;
    unsigned short a = (hextoi.sh<<8)&0xff00;
    unsigned short b = (hextoi.sh>>8)&0x00ff;
    
     unsigned short c=a | b;
    
    printf(">>>>>>> a: %.2x     b: %.2X\n c: %.2X\n",a,b,c);
    
    //hextoi.sh= ((hextoi.sh<<8)&0xff00)|((hextoi.sh>>8)&0x00ff);
    
    printf("Data -> %.2X %.2X , int -> %d\n", hextoi.ch[0],hextoi.ch[1], hextoi.sh);
    
    

    return 0;
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.