Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Byte to Char

Status
Not open for further replies.

thecaptain0220

Programmer
Joined
Dec 12, 2007
Messages
1
Location
US
Ok pretty much what I need to do is convert a byte to hex. Now obviously there is more to it than that. I also know there is no byte type in c++ so i need to figure out what to use as a substitute. Here is what I have right now.
Code:
char strNum;
strNum = (char)0x10;
So basically the character strNum will contain the character created from the byte 00010000. Now my program works great like this. Now I have decided to change the implementation though. Basically 1 or a few bits will need to be changed in the byte. So i need to create somthing to represent the byte. I guess anything really, like an integer array or character array, but I need to know what would probably be best. Then I need to get that into strNum as a character. Im not sure how to do this. I dont know if it would be eaiser to convert it to hexadecimal some way and then cast it to a character or just convert it straight to a character. I dont know if you can even do it the first way since there is no hex data type. So basically in a little overview I need to be able to change any bit in a byte and then store it in a character type. Please let me know how you think I should go about this.
Thank you
 
I'm not quite sure what you're asking, but a char is a byte (although unsigned char might be more appropriate to use as a 'byte' type).
0x10 is exactly the same as 16, so all of these work identically:

Code:
int i   = 16; // or 0x10
long l  = 16; // or 0x10
short s = 16; // or 0x10
char c  = (char)16;

c = (char)i;
c = (char)l;
c = (char)s;
c = (char)0x10;
 
You may write char constants as '\x10' (no need in explicit cast). Really in bit logic ops (as &, |, ~, ^) char values implicitly promoted to int so least 8 bits present byte value.
IMHO, it's sufficient info to form hex value as a char string...
Apropos, no such animal as hex (or binary, or octal and so on;) data type.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top