Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 21st, 2004, 10:05 AM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default how can i convert strings into integers

how can i represent the ASCII code for names...?..or letters?

Reply With Quote
  #2 (permalink)  
Old February 24th, 2004, 02:55 PM
Friend of Wrox
 
Join Date: Feb 2004
Posts: 177
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can convert the string to array of integers, not integer.
i.e. if you have a string "ABC", you can convert the string to its euivalent ASCII array as {65,66,67}.

This you can do directtly as given below.

char *st = "ABC";
int ascii[3];
ascii[0] = st[0];
ascii[1] = st[1];
ascii[2] = st[2];

I think this answers your question.

Regards
Pradeep P


It is not how much we do,
but how much love we put in the doing.

-Mother Theresa
Reply With Quote
  #3 (permalink)  
Old February 27th, 2004, 06:27 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Since some of your previous posts asked about C++ issues, I submit
the following C++ code for your consideration.

Note that the ASCII value of the character 'A' is 65 (decimal).
This bit pattern ("01000001") is stored in char_array[0] by
the initialization of char_array[].

The first "for" loop copies the numerical values to an array
of integers. The numerical value stored in int_array[0] is
the same as that in char_array[0]. The exact way that the
values are stored are (usually) not important to us, as long
as we can get the stuff in and out in a deterministic fashion.

The output statement with cout and char_array[] converts the
internal numerical value to whatever is required to display
the ASCII equivalent of this bit pattern.

The output statement with cout and int_array[] converts the (same)
internal numerical value to whatever is required to display
the decimal integer value of this bit pattern.

These output statements do not change the value of anything in
memory, they convert to whatever representation is defined
in the program.

Code:
#include <iostream>

using namespace std;

int main()
{
  char char_array[ ] = "Ahmed"; // five characters and terminating '\0'
  int  int_array[6];
  int i;

  for (i = 0; i < 5; i++) { // copy the characters to integer
    int_array[i] = char_array[i];
  }

  cout << "Here are the contents of char_array[]" << endl;

  for (i = 0; i < 5; i++) { //cout shows contents, assuming they are chars
    cout <<"<"<<char_array[i]<<">";
  }
  cout << endl << endl;


  cout << "Here are the contents of int_array[]" << endl;

  for (i = 0; i < 5; i++) { //now show integer values
    cout <<"<"<<int_array[i]<<">";
  }
  cout << endl << endl;

  cout << "Here are the contents of char_array[], using (int) cast" 
       << endl;

  for (i = 0; i < 5; i++) {// contents haven't changed; show integer equivalents
    cout <<"<"<<(int)char_array[i]<<">";
  }
  cout << endl << endl;

  cout << "Here are the contents of int_array[], using (char) cast"
       << endl;

  for (i = 0; i < 5; i++) {// now feed cout with char
    cout <<"<"<<(char)int_array[i]<<">";
  }
  cout << endl << endl;

  return 0;
}

Dave
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing strings and integers pcase XSLT 2 December 1st, 2007 06:55 AM
Unsigned integers AndrewH Java Basics 1 September 26th, 2006 02:46 PM
Integers in SQL Server tconlan25 SQL Server 2000 3 August 23rd, 2005 10:08 AM
getting Integers from the query string TadReeves JSP Basics 2 March 16th, 2005 01:22 PM
Sum of All Integers Between 2 numbers bicho Classic ASP Basics 4 July 6th, 2003 01:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.