Convert integer to hexadecimal

后端 未结 1 510
难免孤独
难免孤独 2021-01-26 00:16

I just need to convert integer to hexadecimal. Actually I have a char array in which I am storing hex values.

int var;
var=[self getValue];
char hexValues[5];
he         


        
1条回答
  •  一整个雨季
    2021-01-26 00:32

    a hex value IS an integer value

    hexadecimal is just a way of displaying an integer.

    if you want to create a string that is the hexadecimal representation of an integer, you can go:

    char hexStr[20];
    sprintf(hexStr,"0x%x",var);
    

    you can also pad the number with zeros if you want, by going %0(total number of digits desired)x

    eg:

    int var = 0x123;
    sprintf(hexStr,"0x%08x",var);
    

    will result in hexStr being filled with "0x00000123"

    0 讨论(0)
提交回复
热议问题