Jump to content
  • Advertisement
Sign in to follow this  
Yamian

Data type conversion

This topic is 4736 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hwo can I convert chars to their ascii equivelent, then hex, then back again? what functions are used for this?

Share this post


Link to post
Share on other sites
Advertisement
Language? C? C++? Something else? And are you wanting to print the different representations (thus basically convert them to strings), or just have access to them in code?

Some basic C code to do printing:

void DisplayCharacter(char c)
{
printf("Character: %c\nASCII (dec): %d\nASCII (hex): 0x%02X\n", c, (int)c, (int)c);
}

Share this post


Link to post
Share on other sites
What programming language are you using?

Assuming you're using C/C++ then a char data type is really just a 1 or 2 byte storage area that constains the ASCII value of the character.

To demonstrate this, assign the character to an integer. The value which is in the integer will be the ASCII value of the character.

To convert back and forth between hex and decimal there are formatting function in the standard IO libraries. I generally prefer to use sprintf(), but there are C++ equivalent as well.

Share this post


Link to post
Share on other sites
srry, it is c++

so liek if I want the ascii value of a char just do something like

int a = 'a' and it will get the ascii value of a?

Share this post


Link to post
Share on other sites
Yup. A char data type is basically just a number data type just like int, except that it is (typically) 8 bits instead of 32 bits that int (typically) is. I can't remember, but whether it is signed or not might be compiler-dependent, so it might have a range of -128 to +127, or +0 to +255. But generally, treating a char as a number works perfectly fine. And an extra cast to (unsigned int) can guarantee that it gets interpretted as +0 to +255.

Share this post


Link to post
Share on other sites
You can't actually convert a char to hex. Internally, it's just binary, whether or not you call it a char or an int or something else.
The same binary code can be read as either an integer value or an ASCII character, the current position of the main character in your game, or even a floating point number. But internally, it's all just binary, meaning you can't convert something from ASCII to hex. What you can do, is take a char and read its value as an integer, and then print it out in base 16 (hex).

Quote:


so liek if I want the ascii value of a char just do something like

int a = 'a' and it will get the ascii value of a?

You could just try it to find out. ;)
But yes, that should work.
That will give you the ASCII value. To print it out as hex, instead of decimal numbers, you have to mess about with cout or printf or whatever you use. Remember, it's an int in both cases, and it contains the same value. It's just a matter of telling the function which number base it should use when printing the number.

Share this post


Link to post
Share on other sites
okay thank u very much. this works visa-versa too like

char a = 92 will also equal 'a'? sweet, I'm doing this to encrypt some of my agme files. thx a mil.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!