Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ordinal value

Posted on 2001-08-15
8
Medium Priority
?
959 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
[X]
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
8 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 200 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
Independent Software Vendors: 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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

715 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