954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Converting a char array into numbers

Is is possible to take an array full of characters and then make the characters into numbers?

i.e. a =1, b=2 etc.

I'm trying to write a program that deciphers ceasar shift ciphers.

Any ideas would be really helpful,

Thanks

dj_saxy

dj_saxy
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 
int get_code(char c)
{
  string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return static_cast<int>(alpha.find(c));
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

OK, i guess that it's a function so it should go at the start, but do i need to change anything, because my compiler doesn't like it...

Thanks for the help though

dj_saxy
Newbie Poster
6 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

>because my compiler doesn't like it...
Well, I was assuming some measure of intelligence on your part. I won't make that mistake in the future:

#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

int get_code(char c)
{
  string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return static_cast<int>(alpha.find(c));
}

int main()
{
  string s = "This is a test";

  cout<<"Code values of \""<< s <<"\" are:\n";
  for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
    if (isalpha(*it))
      cout<< left << setw(3) << get_code(*it);
  }
  cout<<endl;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

U can define a function called as " convert "
th prototype will look like this : int convert( char)
for(i=0;i

harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
 
U can define a function called as " convert " th prototype will look like this : int convert( char) for(i=0;i So, do you just post random replies without reading the original question? Come to think of it, is this trick the only one you know? It seems like the only thing you post.
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Consider the string ‘AAAABCCCCCDDDD’ consisting of alphabetic characters only.
This string is of length 14. Since the string consists of alphabetic characters only, duplicate characters can be removed and replaced with duplication factor n. With this technique the string can be compressed and represented by ‘4AB5C4D’.The compressed string is of length 7.
Write a program which takes a string in compressed form and recreates the original uncompressed string.

RAZA009
Newbie Poster
1 post since Jun 2010
Reputation Points: 9
Solved Threads: 0
 

Can you have something like:-

12A3K = AAAAAAAAAAAAKKK

Where the number can be 2 digits or greater.

If not, it should be simple... loop through each char. But this is h/w so post your attempts.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
Is is possible to take an array full of characters and then make the characters into numbers ?

To convert from 1 char to an int just subtract '0' from it. For example :

char ch = '1';
int i = ch - '0'; //i = int(1);


So just apply that to the whole array.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

To convert from 1 char to an int just subtract '0' from it. For example :

char ch = '1';
int i = ch - '0'; //i = int(1);

So just apply that to the whole array.

So all chars to be possible are numbers (0x30 .. 0x39) only ?

Why only should chars be converted into integers, aren't they already integers (0x00 ... 0xff) internally?

-- tesu

tesuji
Master Poster
721 posts since Apr 2008
Reputation Points: 158
Solved Threads: 98
 

So all chars to be possible are numbers (0x30 .. 0x39) only ?

Why only should chars be converted into integers, aren't they already integers (0x00 ... 0xff) internally?

-- tesu

Actually, I mis-read his post. Sorry.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You