Is there a function that returns the ASCII value of a character? (C++)


Question

I need a function that returns the ASCII value of a character, including spaces, tabs, newlines, etc...

On a similar note, what is the function that converts between hexadecimal, decimal, and binary numbers?

1
13
5/10/2009 5:26:03 PM

Accepted Answer

char c;
int ascii = (int) c;
s2.data[j]=(char)count;

A char is an integer, no need for conversion functions.

Maybe you are looking for functions that display integers as a string - using hex, binary or decimal representations?

25
2/6/2014 6:59:32 AM

You don't need a function to get the ASCII value -- just convert to an integer by an (implicit) cast:

int x = 'A';  // x = 65
int y = '\t'; // x = 9

To convert a number to hexadecimal or decimal, you can use any of the members of the printf family:

char buffer[32];  // make sure this is big enough!
sprintf(buffer, "%d", 12345);  // decimal: buffer is assigned "12345"
sprintf(buffer, "%x", 12345);  // hex: buffer is assigned "3039"

There is no built-in function to convert to binary; you'll have to roll your own.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon