Solved

ordinal value

Posted on 2001-08-15
8
954 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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