Char Array to Long array

Hi!
I need to copy the bits from char array to long array.
I have written this, but it returns something else every time:
1
2
3
4
5
long key[4];
char password[16];
cout << "Password:" << endl;
cin >> password; // I know I should use cin.get()
*key = *reinterpret_cast<long *>(password);
Last edited on
You're only putting one long into the array key. If you really want to do this, make key a long pointer (not an array) and replace the last line with

key = reinterpret_cast<long *>(password);

This will make key point to the same data as password. If you don't want this behavior, copy password into a new char array before casting.

You probably shouldn't be using reinterpret_cast this way, though. It might work on some compilers but fail horribly on others, or even just on different optimization levels. (Come to think of it, this specific code might be fine because you're using char; I'm really not sure.)
Last edited on
HI ,
you do not neet the long array and reinterpret_cast for it .
long key;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char password[50];
	bool flag = false ; 
	cout << "Password:" << endl;
	cin >> password; 
	int len = strlen(password) ; 

for( int i = 0 ; i < len; i++)
{
	if( !isdigit(password[i]) )
		flag = true; 
}
if( !flag ) 
{
	key = atol(password);
	cout <<"The key is = "<<key;
}else 
	{
		cout<<"\n Key is not in the proper format ";
	}
Topic archived. No new replies allowed.