Converting an int to a 2 byte hex value in C

后端 未结 15 1401
不知归路
不知归路 2020-12-05 18:17

I need to convert an int to a 2 byte hex value to store in a char array, in C. How can I do this?

15条回答
  •  离开以前
    2020-12-05 18:48

    void intToHex(int intnumber, char *txt)
    {    
        unsigned char _s4='0';
        char i=4;
        //intnumber=0xffff;
    
        do {
            i--;
            _s4 = (unsigned char)  ((intnumber >> i*4 ) & 0x000f);
            if(_s4<10)
                _s4=_s4+48;
            else
                _s4=_s4+55;
    
            *txt++= _s4;    
        } while(i);     
    }
    ...
    char text [5]={0,0,0,0,0};
    ...
    
    intToHex(65534,text);
    USART_Write_Text(text);
    ....
    

提交回复
热议问题