Change bit of hex number with leading zeros in C++,(C)

后端 未结 4 2001
予麋鹿
予麋鹿 2021-01-21 16:24

I have this number in hex string:

002A05.

I need to set 7-th bit of this number to 1, so after conversion I will get

022A05
         


        
4条回答
  •  Happy的楠姐
    2021-01-21 17:12

    You asked about strtol specifically, so to answer your question, just add padding after you convert the number with strtol:

    const char *s = "002A05";
    int x = strtol(s, NULL, 16);
    x |= (1<<17);
    printf("%.6X\n",x);
    

提交回复
热议问题