Solved

C++, how to find a character's matching index within another character array?

Posted on 2009-05-20
4
442 Views
Last Modified: 2012-05-07
Hi all,

I have a character array with base 64 digits "alph" and I have a character array containing a base 64 message, "cTotalImage". My algorithm below works fine, but I was thinking that there MUST be a better way to find a particular character's index within "alph". Does anyone know of a faster general approach to what I'm doing here?

Please let me know if I can clear any of this up! Thanks for your help in advance.

BTW I know there are classes out there that do this, but I'd rather write one of my own so that I can learn and grow my programming knowledge - do not suggest that I go elsewhere for a base 64 decoding class.
char alph[64];

	strcpy(alph, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

	
 

		// Find index of characters in alph

		short iSearch;

		short iDecimals[4] = {0, 0, 0, 0};

		for (iSearch = 0; iSearch < 64; iSearch++)

		{

			if (cTotalImage[iDecodeIndex] == alph[iSearch])

			{

				iDecimals[0] = iSearch;

				break;

			}

		}

		for (iSearch = 0; iSearch < 64; iSearch++)

		{

			if (cTotalImage[iDecodeIndex + 1] == alph[iSearch])

			{

				iDecimals[1] = iSearch;

				break;

			}

		}

		// etc...

Open in new window

0
Comment
Question by:ehensens
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24436008
// if you are going to do it many times, initialize
int index[CHAR_MAX+1];

for( int i;i<64;i++){
   index[alpha[i] = i;
}

then

for( i=0;i<4; i++ ){
  decimals[i] = index[cTotalImage[iDecodeIndex+i]];
}
0
 

Author Comment

by:ehensens
ID: 24436061
Hi ozo,

Thank you for your reponse. I cannot quite understand what you're doing (sorry), could you please add comments and fix the index[alpha[i] = i; part? Thanks!
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24436085
You can calculate the index if you take into accout that the alphabet is consecutive and the ASCII characters are also, e.g.
#include <ctype.h>
 

// ...
 

if (isupper(cTotalImage[iDecodeIndex]) { // uppercase character
 

  iDecimals[0] = cTotalImage[iDecodeIndex] - 65; // ASCII 'A' == 65

}
 

if (islower(cTotalImage[iDecodeIndex]) { // lowercase character
 

  iDecimals[0] = cTotalImage[iDecodeIndex] - 97 + 25; // ASCII 'a' == 97, add offset until 'Z' in your array

}
 

if (isdigit(cTotalImage[iDecodeIndex]) { // 0 - 9
 

  iDecimals[0] = cTotalImage[iDecodeIndex] - 48 + 50; // ASCII '0' == 97, add offset until 'z' in your array

}
 

// add special treatment for +/

Open in new window

0
 

Author Closing Comment

by:ehensens
ID: 31583702
I like this approach, good thinking.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 84
I could not build boost code, 10 78
Add values of each row in an array 3 50
Issues with C++ Class 19 80
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now