Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to convert char type to 16 binary string?
for example as below:

if variable temp is char type,and it's ini value is 255,
i wanne get "FF".

so how to convert ?
thanks for relpy.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 10:36am    
What does it mean, "16 binary string"? :-)
—SA

1 solution

You may use sprintf[^] (or printf, depending on your needs for such a purpose), e.g.
C
#include <stdio.h>

int main()
{
  unsigned char u = 255;

  printf("%02X\n", u);

  return 0;
}


You might as well code an ad hoc function yourself.
 
Share this answer
 
Comments
xuyunhai 11-Jan-16 3:51am    
int itemp = (int)recbuf;
char *temp = 0;
itoa(itemp,temp,16);

recbuf is unsigned char array,why i can't normal run and throw exception?
thanks.
CPallini 11-Jan-16 3:55am    
You got an exception because you have allocated NO memory for temp.
If you want to show each received character then you have to do something like
int itemp = recbuf[k]; // itemp is 0..255
char temp[3]; // two hexadecimal digits plus string terminator
itoa(itemp, temp); // now temp holds the string representation of recbuf[k].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900