Solved

ordinal value

Posted on 2001-08-15
8
943 Views
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
0
Comment
Question by:Lmoen
8 Comments
 
LVL 6

Accepted Solution

by:
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.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6388786
better example

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

Expert Comment

by:Axter
ID: 6388787
Example:

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

or

char SomeStr[] = "Hello World";
int ASC_Value = SomeStr[0];//makes it equal to 'H'
0
 
LVL 30

Expert Comment

by:Axter
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
}
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
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 );
}

0
 
LVL 6

Expert Comment

by:Triskelion
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'));
0
 
LVL 86

Expert Comment

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

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');

0
 
LVL 22

Expert Comment

by:nietod
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;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now