Conversion error?

Ok, so I thought I was done with my program, but it seems I have another error, which, for the life of me, I cannot find out why!

I have this following code:
1
2
3
4
5
6
7
8
9
TCHAR *keyTchar = new TCHAR[ App::szKey ];
int *key = new int[ App::szKey ];

//NULLify the key variables
for( int i = 0; i < App::szKey; ++i )
{
	key[ i ] = NULL;
	keyTchar[ i ] = NULL;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//create a key( digit ) counter
int keyCount = 0;

//convert from TCHAR to int
for( int i = 0; i < App::szKey; ++i )
{
	if( keyTchar[ i ] == NULL )
		break;

	key[ i ] = _ttoi( &keyTchar[ i ] );

	++keyCount;
}

//create an index counter
int index = keyCount;

////////Break the key in to single digits/////////
for( int i = 0; i < index; ++i )
{
	for( int j = 0; j < ( keyCount - 1 ); ++j )
		key[ i ] /= 10;

	--keyCount;
}
////////////////////////////////////////////////// 


Which will take a key from TCHAR and convert it to an int. This works fine, until I type a message that generates 10 or more keys.

Here's a log file the program generates:
ENCRYPT button pressed
Message before encryption: Enter text here, to be encrypted / decrypted...
Message after encryption:  Nwcna cngc mjwj" yt kn nwlahycji % ijhwdqufe///

Key int:   2 1 2 1 4 
Key TCHAR: 2 1 2 1 4 

DECRYPT button pressed
Message before decryption: Nwcna cngc mjwj" yt kn nwlahycji % ijhwdqufe///
Message after decryption:  Enter text here, to be encrypted / decrypted...

Key int:   2 1 2 1 4 
Key TCHAR: 2 1 2 1 4 
=============================================================================================================
ENCRYPT button pressed
Message before encryption: aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff
Message after encryption:  ffffffffffkkkkkkkkkkggggggggggmmmmmmmmmmffffffffffoooooooooo

Key int:   1 2 3 2 4 2 
Key TCHAR: 1 2 3 2 4 2 

DECRYPT button pressed
Message before decryption: ffffffffffkkkkkkkkkkggggggggggmmmmmmmmmmffffffffffoooooooooo
Message after decryption:  aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffff

Key int:   1 2 3 2 4 2 
Key TCHAR: 1 2 3 2 4 2 
=============================================================================================================
ENCRYPT button pressed
Message before encryption: aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggg
Message after encryption:  bbbbbbbbbbkkkkkkkkkkddddddddddhhhhhhhhhhffffffffffkkkkkkkkkknnnnnnnnnn

Key int:   4 2 4 3 4 1 5 
Key TCHAR: 4 2 4 3 4 1 5 

DECRYPT button pressed
Message before decryption: bbbbbbbbbbkkkkkkkkkkddddddddddhhhhhhhhhhffffffffffkkkkkkkkkknnnnnnnnnn
Message after decryption:  aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggg

Key int:   4 2 4 3 4 1 5 
Key TCHAR: 4 2 4 3 4 1 5 
=============================================================================================================
ENCRYPT button pressed
Message before encryption: aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhh
Message after encryption:  hhhhhhhhhhgggggggggglllllllllleeeeeeeeeeiiiiiiiiiiggggggggggkkkkkkkkkkqqqqqqqqqq

Key int:   5 1 2 4 3 4 3 2 
Key TCHAR: 5 1 2 4 3 4 3 2 

DECRYPT button pressed
Message before decryption: hhhhhhhhhhgggggggggglllllllllleeeeeeeeeeiiiiiiiiiiggggggggggkkkkkkkkkkqqqqqqqqqq
Message after decryption:  aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhh

Key int:   5 1 2 4 3 4 3 2 
Key TCHAR: 5 1 2 4 3 4 3 2 
=============================================================================================================
ENCRYPT button pressed
Message before encryption: aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiii
Message after encryption:  hhhhhhhhhhgggggggggglllllllllliiiiiiiiiiffffffffffmmmmmmmmmmlllllllllloooooooooonnnnnnnnnn

Key int:   5 1 2 1 4 5 1 5 1 
Key TCHAR: 5 1 2 1 4 5 1 5 1 

DECRYPT button pressed
Message before decryption: hhhhhhhhhhgggggggggglllllllllliiiiiiiiiiffffffffffmmmmmmmmmmlllllllllloooooooooonnnnnnnnnn
Message after decryption:  aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiii

Key int:   5 1 2 1 4 5 1 5 1 
Key TCHAR: 5 1 2 1 4 5 1 5 1 
=============================================================================================================
ENCRYPT button pressed
Message before encryption: aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj
Message after encryption:  hhhhhhhhhhggggggggggddddddddddkkkkkkkkkkffffffffffkkkkkkkkkknnnnnnnnnnllllllllllnnnnnnnnnnnnnnnnnnnn

Key int:   5 1 4 5 4 1 5 3 1 3 
Key TCHAR: 5 1 4 5 4 1 5 3 1 3 

DECRYPT button pressed
Message before decryption: hhhhhhhhhhggggggggggddddddddddkkkkkkkkkkffffffffffkkkkkkkkkknnnnnnnnnnllllllllllnnnnnnnnnnnnnnnnnnnn
Message after decryption:  yyyyyyyyyybbbbbbbbbbccccccccccddddddddddeeeeeeeeeeffffffffffgggggggggghhhhhhhhhhiiiiiiiiiijjjjjjjjjj

Key int:   2 1 4 5 4 1 5 3 1 3 
Key TCHAR: 5 1 4 5 4 1 5 3 1 3 
=============================================================================================================


Note the last entry above. What ever digit is in the first element of key, will always be converted to 2 when the keyCount is >= 10.

I can't seem to figure out why this is happening. If anyone can help, I'd be very greatful!

Thanks.
First instinct says to look over the effect that
key[ i ] /= 10;
is having on your key int. It might be breaking the for loops.

Aside from that, maybe there is code elsewhere that might effect it?
Ok, so I found out why it was doing it! I should have done a debug before posting this, but I still need help with it anyway.

When I'm converting to int:
1
2
3
4
5
6
7
8
9
10
//convert from TCHAR to int
for( int i = 0; i < App::szKey; ++i )
{
	if( keyTchar[ i ] == NULL )
		break;
						
	key[ i ] = _ttoi( &keyTchar[ i ] );
						
	++keyCount;
}


I'm using _ttoi() and passing the address, which is reading all the proceeding elements of the TCHAR. By doing this, I'm hitting the top range, 2147483647.

So when it's broken down, I'm always ending with a 2, until the TCHAR can go in to the int without hitting the top range.

So, is there a way to do this without using the current address of the TCHAR, so that they are single digits?

Or is it possible to use GetDlgItemInt() and store each digit singly in to an array?
I no longer need help with this. Don't know why I never thought of this in the first place...

1
2
3
4
5
for( int i = App::szKey; i >= 0; --i )
{
	if( keyTchar[ i ] != NULL )
		key[ i ] = ( int )keyTchar[ i ] - 48;
}


If anyone finds themselves in a similar situation.
All I have done, is typecast the TCHAR to an int and minus 48 to get the corresponding number from the ascii table.
http://www.asciitable.com/

The number 3 on the ascii table has a value of 51 when typecast to an int. -48 gives you the number you had in the TCHAR.
Topic archived. No new replies allowed.