Solved

ordinal value

Posted on 2001-08-15
8
949 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

813 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

12 Experts available now in Live!

Get 1:1 Help Now