ordinal value

Posted on 2001-08-15
Last Modified: 2011-09-20
just wondering, how do I get the ordinal value for an integer in C++.  I basically just moved from Delphi to C++.  I know it's for example, Ord(i) in Delphi, to get the ASCII value of an integer. (if I remember correctly)

how do I get the ASCII value in C++?  (Using C++ Builder 5)

Thanks for the help
Question by:Lmoen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Triskelion earned 50 total points
ID: 6388770
In C and C++, many of the simple types ARE the ordinal values depeding on how you treat them.

For instance, if you
printf("%d", '0'); // decimal
you will get it's ASCII value.

if you
printf("%c", 48);//character
you will get the character representation of the number.

Expert Comment

ID: 6388786
better example

printf("%c %d", '0','0');
will print
   0 48
(in an ASCII evironment)
LVL 30

Expert Comment

ID: 6388787

int var = 65; //65 equals 'A';
char SomeStr[5]= "";
SomeStr[0] = var;


char SomeStr[] = "Hello World";
int ASC_Value = SomeStr[0];//makes it equal to 'H'
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 30

Expert Comment

ID: 6388794
The following is an example on how to convert a number to a std::string

#include <sstream>
#include <string>

template <typename T>
std::string Ttos(T arg)
   std::ostringstream buffer;
   buffer << arg; // send to the ostringstream
   return buffer.str(); // capture the string
LVL 30

Expert Comment

ID: 6388806
Some compilers support a NON-standard function call itoa();

Example code:

#include <stdlib.h>
#include <stdio.h>

void main( void )
   char buffer[20];
   int  i = 3445;
   long l = -344115L;

   _itoa( i, buffer, 10 );
   printf( "String of integer %d (radix 10): %s\n", i, buffer );
   _itoa( i, buffer, 16 );
   printf( "String of integer %d (radix 16): 0x%s\n", i, buffer );
   _itoa( i, buffer, 2  );
   printf( "String of integer %d (radix 2): %s\n", i, buffer );


Expert Comment

ID: 6388812
//please forgive the choppiness of this, I'm being distracted

If you want to perform math on numeric characters,
you can subtract '0' from the numeric character.

char chrSeven='7';
char chrFive='5';

int intNewVal= ((chrSeven - '0') + (chrFive - '0'));
LVL 86

Expert Comment

ID: 6388826
>>how do I get the ASCII value in C++?  (Using C++ Builder

If I remember right, 'ORD()' returns the ASCII value for a character (not an integer). In C/C++ there's a direct correspondance between a char ant it's ASCII code, so by using

int nASCII = ( int) 'a';

'nASCII' will have the ASCII code for 'a'  -not keeping in mind the different codepages.

To use a library function that takes care of that, use '__toascii()':

#include <ctype.h>

int nASCII = __toascii('a');

LVL 22

Expert Comment

ID: 6388835
In C and C++, unlike in Pascal and Delphi, the character type is an integral type.  That is, it stores an integer value.  So in C/C++ you can do things like

char x = 1; // stores an interal 1.
char y = 'A'; // Stores an integral 65.
char z = x + y; // stores 66, or 'B'
char i = x*y; // stores 60, 'A'
char j = z*x; // tries to store 4290, this might overflow a char though.

So there is no need to convert an character value to an integral type, it already is an integral type.  That is why you can do.

int i = 100;
char c = 'a';

i = c;
c = i;

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 320
C++ :Change value from  DisableCMD registry 4 80
designing in object programming 12 132
boost::uuid crashes 17 63
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question