how to print signed hex in c

前端 未结 3 2002
醉酒成梦
醉酒成梦 2020-12-18 19:46

I know we can use printf(\"%04X\", value); to print unsigned hex values

is there a similar flag or a function in c that you can use to print signed hex values?

3条回答
  •  囚心锁ツ
    2020-12-18 20:11

    No, but you can do something like

    printf("%c%04X",(x<0)?'-':' ',(x<0)?-x:x);
    

    But, as other point out, it is doubtful whether there is a valid reason to do so. Be sure to understand exactly what it is that you're asking for.

    EDIT: according to the edit to your post, you do understand what you're asking for, so it's all your fault ;-)

提交回复
热议问题