Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > C++

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 11-05-2009, 05:34 AM
c-ray's Avatar
c-ray c-ray is offline
Registered User
 
Join Date: Nov 2009
Posts: 4
Question How to get the representation of ASCII Charcters from a String Array?

Dear programmer,

I am searching for a way to get the ASCII character representation from a string array. I am doing a project on stacks.

An example:[u]

Code:
string *myArray;
myArray = new string[arraySize];
*Now the above string array consists of characters (e.g: myArray[0] = 1, myArray[1] = *, myArray[2] = 45...etc).

The idea is get the ASCII code of those characters that are stored in the string array.

Code:
int( myArray[0] ); //Will not work as it is an invalid cast.
*If any one can provide me of any ideas, i would be grateful... I am a beginner-to-intermediate C++ programmer. biggrin.gif

Thanks for reading,
My Regards
Reply With Quote
  #2  
Old 11-05-2009, 08:28 AM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
You have, from a char array standpoint, a 2-d array now because myarray added a dimension. If you were using string_variable[0] you now need:

myarray[0][0];

And that should do the trick. For the second string, its

myarray[1][0];

and for the second letter of the second string:

myarray[1][1];

etc.
Reply With Quote
  #3  
Old 11-05-2009, 09:46 AM
c-ray's Avatar
c-ray c-ray is offline
Registered User
 
Join Date: Nov 2009
Posts: 4
Question Re: How to get the representation of ASCII Charcters from a String Array?

Thanks for your reply...

*Technically, here is the problem:

Code:
string *myArray;
myArray = new string[arraySize];

//Here there is a series of functions ending up with a combination of //integers or characters that are stored in myArray.

//As a result, myArray[0] for example can only contain an integer or one character. My goal at this point is to print the ASCII code representation of  
the first character encountered in the string array element.

//An Example: if myArray[0] == 15, i want the program to print the ASCII code of (1)... truncating the string value to one character. 

//using (atoi) function will also report an error as it is not intended for converting string array elements to integers.

for(int k = 0; k < arraySize; k++)
{
    cout << "The ASCII code of: " << myArray[k] << " is: " 
           << int(myArray[k]) << endl; 

//Error: Will generate an error of invalid cast because myArray type is a string.

}
*From what i understood, you are suggesting to declare a two dimensional array in order to cast the first character in the string array element and retrieve its ASCII code representation. Is that what you meant?

*Thanks anyway for your help. :) I hope i clarified the problem in a more efficient way.

*You may be wondering what is the catch of such a useless program, you are right, it is just a practicing exercise in stacks, i am searching for the most efficient algorithm to build up my program. Till now i testing each unit alone to make sure everything is working properly, and then i found this fatal error in my code when i assembled all the header files and units together.

Thanks for reading :D
Reply With Quote
  #4  
Old 11-05-2009, 12:47 PM
c-ray's Avatar
c-ray c-ray is offline
Registered User
 
Join Date: Nov 2009
Posts: 4
Lightbulb [Solved] How To Get The Representation Of ASCII Characters From A String Array

Here is the solution:

[code]

string *myArray;
myArray = new string[arraySize];

//Here there is some functions are declarations which passes values to (myArray).

for(int k = 0; k < arraySize; k++)
{
cout << "The ASCII code of: " << myArray[k] << " is: " << static_cast<int>((myArray[k]).at(0)) << endl;
}

[code]

*This will cast the first character in the current element of the string array as an integer -(The ASCII Code will be displayed accordingly) The characters that are followed in the same element are not in our interest. As this algorithm is used to detect integers, characters and Boolean expressions.

[This Topic is Solved]
Reply With Quote
  #5  
Old 11-05-2009, 04:17 PM
Danny's Avatar
Danny Danny is offline
Super Moderator
 
Join Date: Nov 2003
Posts: 3,948
You don't really need the at() member function. This code:
Code:
static_cast<int>((myArray[k]).at(0)
Can be replaced with
Code:
cout<<(int)myArray[k][0];
Or if you find static_cast more useful (I don't) then:
Code:
cout<<static_cast<int>(myArray[k][0]);
This will print the decimal ASCII value of the char at position myArray[k][0].
__________________
Danny Kalev

Last edited by Danny; 11-05-2009 at 04:21 PM.
Reply With Quote
  #6  
Old 11-05-2009, 04:32 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
Quote:
Originally Posted by c-ray View Post
Thanks for your reply...

*Technically, here is the problem:

Code:
string *myArray;
myArray = new string[arraySize];

//Here there is a series of functions ending up with a combination of //integers or characters that are stored in myArray.

//As a result, myArray[0] for example can only contain an integer or one character. My goal at this point is to print the ASCII code representation of  
the first character encountered in the string array element.

//An Example: if myArray[0] == 15, i want the program to print the ASCII code of (1)... truncating the string value to one character. 

//using (atoi) function will also report an error as it is not intended for converting string array elements to integers.

for(int k = 0; k < arraySize; k++)
{
    cout << "The ASCII code of: " << myArray[k] << " is: " 
           << int(myArray[k]) << endl; 

//Error: Will generate an error of invalid cast because myArray type is a string.

}
*From what i understood, you are suggesting to declare a two dimensional array in order to cast the first character in the string array element and retrieve its ASCII code representation. Is that what you meant?

*Thanks anyway for your help. :) I hope i clarified the problem in a more efficient way.

*You may be wondering what is the catch of such a useless program, you are right, it is just a practicing exercise in stacks, i am searching for the most efficient algorithm to build up my program. Till now i testing each unit alone to make sure everything is working properly, and then i found this fatal error in my code when i assembled all the header files and units together.

Thanks for reading :D
No, its already a 2-d array, sort of. If you think of a string as an array of chars (or shorts if you are in unicode), then a string* adds a dimension, so its already 2-d (you did this, not me!).

I was saying you are trying to access it by myarray[0]; /// <-------- this is a string
instead of myarray[0][0]; //<------- this is the first char in the string.

The first one cannot be converted into an int directly (well, without brute force) --- if the text represented an integer, you need atoi or some sort of stream gibberish to get it back. You cannot convert myarray[0], which is a string, into much of anything useful with just simple casts. If you go on into that string with myarray[0][X]; you are back to simple types again (char, or short) and it will convert into an integer with just a cast.

Or, danny explained what I meant above. I am not great with strings -- I do a LOT of floating point math and the inability to put a number into a string without nonsense prevents me from using them a lot.

Your fixed/working code is fine, although very wordy for the task at hand.
Reply With Quote
  #7  
Old 11-05-2009, 04:39 PM
jonnin jonnin is offline
Senior Member
 
Join Date: Dec 2003
Posts: 3,008
A side note:
this code will not meet your stated goal. The first char could be a '-' or, rarely, a '+' sign. It could be a 3, but the text could be 3A (hex) or 3.14 (float) or 31 (int) or 3-D (text). If the first value is '-' it could be anything, a float, ascii bullet, hyphen, or other things. There is no way to tell, from a single character, if you have an integer or boolean etc. If you have foreknowledge of your input strings, its possible your algorithm will be able to tell, only you know that, but you want to make sure this is the case.
Reply With Quote
  #8  
Old 11-05-2009, 06:12 PM
c-ray's Avatar
c-ray c-ray is offline
Registered User
 
Join Date: Nov 2009
Posts: 4
Smile Thanks!

Thanks guys for your help. I really appreciate the time that you spend in answering my post. I fully understand the concept now. Sorry for my misunderstanding at first.

*Indeed you were right, by using a pointer, it became some sort of two dimensional array.

Quote:
A side note:
this code will not meet your stated goal. The first char could be a '-' or, rarely, a '+' sign. It could be a 3, but the text could be 3A (hex) or 3.14 (float) or 31 (int) or 3-D (text). If the first value is '-' it could be anything, a float, ascii bullet, hyphen, or other things. There is no way to tell, from a single character, if you have an integer or boolean etc. If you have foreknowledge of your input strings, its possible your algorithm will be able to tell, only you know that, but you want to make sure this is the case.
*I agree with you in the above sentences. But you see, The application is an inverse polish notation calculator and i used the above code after tokenizing the input and splinting it into separate characters. The choice of choosing a string array came from the idea of future development; enabling Boolean evaluation. I am using stacks to continue processing the program, it is working perfectly. I made here an imaginary example to describe my problem whether my code is completely different from the one you see in this post. I intended to keep it very simple, clear and to the point for clarity purposes while posting it. In addition, if people are visiting the forum will probably benefit from the specific idea instead of tacking with many lines of codes without receiving anything.

I would like to give special thanks to you (jonnin) and (Danny) for your support here... What makes a forum a good one is the moderators who are leading it; My best wishes for you, and hope to hear from you in other posts.

My Regards,
C-Ray
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Submitting Infopath to a .net Web Service athomas42 .NET 1 06-25-2007 05:54 PM
App Object Rob Teixeira .NET 15 05-31-2002 04:30 PM
Please help me -- urgent -- deadlock error chandra VB Classic 0 06-22-2000 08:36 AM
Urgent please... --- getting deadlock error..when using activex dll in VC++ Chandra VB Classic 0 06-22-2000 08:30 AM
Database problems Robert Rieth VB Classic 1 04-11-2000 04:21 AM


All times are GMT -4. The time now is 11:17 PM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.