The Python ord() function is a built-in function that returns the Unicode code point of a given character. It takes a single argument, which can be a string of length 1 or a Unicode character. The returned value is an integer representing the Unicode code point of the character. The ord() function is useful when working with Unicode strings and characters, as it allows you to convert characters to their corresponding code points, which can then be used for various operations such as sorting, searching, and encoding. Keep reading below to learn how to python ord in C++.
Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Python ‘ord’ in C++ With Example Code
Python’s `ord()` function returns the Unicode code point of a given character. In C++, there is no built-in function that does exactly the same thing, but it is possible to achieve the same result using a typecast.
To get the Unicode code point of a character in C++, you can simply cast the character to an `int` type. Here’s an example:
“`cpp
char c = ‘A’;
int code = static_cast
“`
In this example, the character `’A’` is cast to an `int` type, which results in the Unicode code point of the character being stored in the `code` variable.
If you want to write a function that behaves like Python’s `ord()` function, you can do so by taking a `char` parameter and returning an `int`:
“`cpp
int ord(char c) {
return static_cast
}
“`
This function takes a `char` parameter and returns the Unicode code point of the character as an `int`.
Using this function is as simple as calling it with a character:
“`cpp
char c = ‘A’;
int code = ord(c);
“`
In this example, the `ord()` function is called with the character `’A’`, which results in the Unicode code point of the character being stored in the `code` variable.
In summary, while C++ doesn’t have a built-in function that behaves exactly like Python’s `ord()` function, it is possible to achieve the same result using a typecast.
Equivalent of Python ord in C++
In conclusion, the equivalent function to Python’s ord() in C++ is the static_cast
Elevate your software skills
Ergonomic Mouse![]() |
Custom Keyboard![]() |
SW Architecture![]() |
Clean Code![]() |