string to byte ?

Hi,
Look my code and tell me if there is a more correct way to do the same.
I need to type 4 byte hex, for example, 0x10, 0x20, 0x30 and 0x40.
I'm using scanf, I type 10 (enter), 20 (enter) 30 (enter) and 40 (enter).
But i would like to type 10203040 and use only one (enter).
But how to turn the string 10203040 in bytes 0x10, 0x20, 0x30 and 0x40?

[ ] ´s

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  /* My Code

1- Inicial Key vector = 0x00,0x00,0x00,0x00,0xAA,0xBB,0xCC,0xDD
2- Digit values for AUX_[0]=10, AUX_[1]=20, AUX_[2]=30, AUX_[3]=40,
3- Final Key vector = 0x10,0x20,0x30,0x40,0xAA,0xBB,0xCC,0xDD
*/

#include <cstring>
#include <cstdlib>
#include <cstdio>


typedef unsigned char  byte;

byte KEY_[8] = {0x00,0x00,0x00,0x00,0xAA,0xBB,0xCC,0xDD};
byte aux_[4];


int main( )
	
{
	printf("Enter AUX Vector :\n");

	for (int i= 0; i < 4; ++i)
	{
		scanf ("%X",&aux_[i]);
	}

	printf ("\n\n");	
	
	printf ("Vector AUX : %X %X %X %X \n", aux_[0], aux_[1], aux_[2], aux_[3]);

	for (int i= 0; i < 4; ++i)
	{
		KEY_[i]=KEY_[i]^aux_[i];
	}

	printf ("\nFinal Vector Key :");
	for (int i= 0; i < 8; ++i)
	{
		printf ("%02X", KEY_[i]);
	}

	printf ("\n\n");
	
	system("pause");

return(0);

}
I'm using scanf,

Why are you using C-stdio functions in this C++ program? Using the C++ streams would solve one of your current problems, your scanf() call is using the wrong format specifier for the type. The "%X" specifier is used for an unsigned int.

If you want to enter the whole "number" at once perhaps you should think about using a string, then parse that string into the desired numbers.

Topic archived. No new replies allowed.