Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Byte to Char

Byte to Char

Byte to Char

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.


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

RE: Byte to Char

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:


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;

RE: Byte to Char

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close