Literal array in int array

Hi, I'm trying to make a literal array in int array. But there's something wrong in my code. Can you help me?

[]´s Pomodoro

// Literal Array to Int Array

#include <string.h>
#include <iostream>
#include <stdlib.h>


char *D[11] =
{
"210743AAAEC8E34B",
"7D16AE8F0E67A37E",
"3A3B504AE96F6147",
"A58ADFCA8EB3A217",
"649821C088CD195A",
"DBDBB68209898D44",
"EC1FBB44455CD8D0",
"D2545542803C5F82",
"095720F6301B3065",
"7A4D0B2607D72E55",
"A29B364DF6469A48"
};

long long int F[11];

//char *str_1 = (char*)malloc(16*sizeof(char));
char str_1[16];

main( )

{

int i;
printf("Part_1-->\n\n");
for (i = 0; i < 11; ++i)
{
strcpy(str_1, D[i]);
F[i] = strtoll (str_1, NULL, 16);
printf("Vetor : %d - %s - %llX \n", i, D[i], F[i]);
}

return(0);
}
do you want to replace each character to its ascii decimal equivalent?
or its ascii hexadecimal equivalent?

for example the character '2' will convert to 50 decimal or 32 hex

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <sstream>

using namespace std;

char *D[11] = {
"210743AAAEC8E34B",
"7D16AE8F0E67A37E",
"3A3B504AE96F6147",
"A58ADFCA8EB3A217",
"649821C088CD195A",
"DBDBB68209898D44",
"EC1FBB44455CD8D0",
"D2545542803C5F82",
"095720F6301B3065",
"7A4D0B2607D72E55",
"A29B364DF6469A48"
};

int main()
{
	// storage for converted values
	vector<vector<int>> int_arrays;

	bool option1 = false;

	if(option1 == true)
	{
		// iterate through each row
		for (auto i : D)
		{
			// output array
			std::cout << i << std::endl;
			
			// temporary storage
			vector<int>		int_array;

			// go through each character			
			for (int j=0; j < 16; j++)
			{				
				// convert char to int and store in vector
				int_array.push_back((int)i[j]);
			}

			// put converted value in storage
			int_arrays.push_back(int_array);
		}

	}
	else
	{
		// iterate through each row
		for (auto i : D)
		{
			// output array
			std::cout << i << std::endl;
		
			std::string string_temp(i);
			//temporary storage
			vector<int>		int_array;

			// go through each character
			for (int j=0; j < 16; j++)
			{
				std::stringstream temp;
				// convert to hex
				temp << std::hex << (int) string_temp[j];

				signed int temp_int;
				// convert string to int
				stringstream(temp.str()) >> temp_int;

							 
				int_array.push_back(temp_int);
			}

			// put converted value in storage
			int_arrays.push_back(int_array);
		}		
	}

	std::cout << std::endl;
	// start of display stored information

	// go through each row
	for (auto xx : int_arrays)
	{
		// go through each column
		for(int i=0; i < xx.size(); i++)
		{				
			std::cout << xx[i]; 
		}

		std::cout << std::endl;
	}

   int x;
   cin >> x;
   return 0;
}
Last edited on
Hi, thanks for your replay,
I´´m trying to do this;

literal D[0] = "210743AAAEC8E34B" will be change to long long int F[0] = 0x210743AAAEC8E34B
.
.
.
literal D[10] = "A29B364DF6469A48" will be long long int F[10] = 0xA29B364DF6469A48

Work only when i=0, 1 and 2 F[i], after return wrong numbers.

[ ] ´s Pomodoro
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <sstream>
#include <cstdint>
#include <stdint.h>
#include <stdio.h>
#include <limits>
#include <bitset>

using namespace std;

string GetBinaryStringFromHexString (string sHex)
		{
			string sReturn = "";
			for (int i = 0; i < sHex.length (); ++i)
			{
				switch (sHex [i])
				{
					case '0': sReturn.append ("0000"); break;
					case '1': sReturn.append ("0001"); break;
					case '2': sReturn.append ("0010"); break;
					case '3': sReturn.append ("0011"); break;
					case '4': sReturn.append ("0100"); break;
					case '5': sReturn.append ("0101"); break;
					case '6': sReturn.append ("0110"); break;
					case '7': sReturn.append ("0111"); break;
					case '8': sReturn.append ("1000"); break;
					case '9': sReturn.append ("1001"); break;
					case 'A': sReturn.append ("1010"); break;
					case 'B': sReturn.append ("1011"); break;
					case 'C': sReturn.append ("1100"); break;
					case 'D': sReturn.append ("1101"); break;
					case 'E': sReturn.append ("1110"); break;
					case 'F': sReturn.append ("1111"); break;
				}
			}
			return sReturn;
		}

int main()
{
	vector<string> values;
	vector<unsigned long long> new_array;

	values.push_back(string("210743AAAEC8E34B"));
	values.push_back(string("7D16AE8F0E67A37E"));
	values.push_back(string("3A3B504AE96F6147"));
	values.push_back(string("A58ADFCA8EB3A217"));
	values.push_back(string("649821C088CD195A"));
	values.push_back(string("DBDBB68209898D44"));
	values.push_back(string("EC1FBB44455CD8D0"));
	values.push_back(string("D2545542803C5F82"));
	values.push_back(string("095720F6301B3065"));
	values.push_back(string("7A4D0B2607D72E55"));
	values.push_back(string("A29B364DF6469A48"));			
						  	
	//std::cout << GetBinaryStringFromHexString(values[0]) << std::endl;
	for (int i=0; i < values.size(); i++)
	{
		std::cout << values[i] << std::endl;
               
               // convert hex string to bits
		bitset<64> b(GetBinaryStringFromHexString(values[i]));
	
               //check to see if the bit pattern is correct
               std::cout << b << std::endl;

		new_array.push_back(b.to_ullong());
	}

	std::cout << endl;

        // output int array converted to hex
	for (auto i: new_array)
	{
		std::cout << setw(16) << setfill('0') << hex << i << std::endl;
	}
	
	int x;
	cin >> x;
}
	
Last edited on
I appreciate your trying to help me but I´m so beginner to understand your code.
Can you help me with my code? Where is my mistake? Please try to compile my code and see the result.

[ ] ´s Pomodoro

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
// Array literal to Array Int

#include <string.h>
#include <iostream>
#include <stdlib.h> 


char *D[11] = 
{
"210743AAAEC8E34B",
"7D16AE8F0E67A37E",
"3A3B504AE96F6147",
"A58ADFCA8EB3A217",
"649821C088CD195A",
"DBDBB68209898D44",
"EC1FBB44455CD8D0",
"D2545542803C5F82",
"095720F6301B3065",
"7A4D0B2607D72E55",
"A29B364DF6469A48"
};

long long int F[11];

char str_1[16];

main( )

{
	int i;
	
	for  (i = 0; i < 11; ++i) 
   	{
		printf("%s\n",D[i]);
	}

	printf("\n\n");

	for  (i = 0; i < 11; ++i) 
   	{
	   strcpy(str_1, D[i]);
    	F[i] = strtoll (str_1, NULL, 16);
		printf("%llX \n",F[i]);
	}
 
 	printf("\n\n");
 
 return(0);
}
i'm using visual studio and i don't have strtoll

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
52
53
54
55
56
57
58
59
60
61

/*replace 
#include <string.h>
#include <iostream>
#include <stdlib.h> */ 

// with 

#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

char *D[11] = 
{
"210743AAAEC8E34B",
"7D16AE8F0E67A37E",
"3A3B504AE96F6147",
"A58ADFCA8EB3A217",
"649821C088CD195A",
"DBDBB68209898D44",
"EC1FBB44455CD8D0",
"D2545542803C5F82",
"095720F6301B3065",
"7A4D0B2607D72E55",
"A29B364DF6469A48"
};

long long int F[11];

char str_1[16];

// use void main or int main
int main( )
{
	int i;
	
	for  (i = 0; i < 11; ++i) 
   	{
		printf("%s\n",D[i]);
	}

	printf("\n\n");

	for  (i = 0; i < 11; ++i) 
   	{
	   strcpy(str_1, D[i]);

// you just need unsigned long long 
    	F[i] = _strtoui64 (str_1, NULL, 16);
		printf("%llX \n",F[i]);
	}
 
 	printf("\n\n");
 
	int x; 
	cin >> x;

 return(0);
}
Some of the values are out of the range of long long
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
52
// Array literal to Array Int

#include <cstring> // <string.h>
#include <iostream>
#include <cstdlib> // <stdlib.h>
#include <iomanip> // std::setw

const int SZ = 11 ;

const char* D[SZ] = //char *D[11] = // **** must be const
{
    "210743AAAEC8E34B",
    "7D16AE8F0E67A37E",
    "3A3B504AE96F6147",
    "A58ADFCA8EB3A217",
    "649821C088CD195A",
    "DBDBB68209898D44",
    "EC1FBB44455CD8D0",
    "D2545542803C5F82",
    "095720F6301B3065",
    "7A4D0B2607D72E55",
    "A29B364DF6469A48"
};

long long F[SZ] = {} ; //long long int F[11];

//char str_1[16];

int main() //main( )
{
    std::cout << "std::strtoll\n------------\n" ;
    for( int i = 0 ; i < SZ ; ++i )
    {
        F[i] = std::strtoll( D[i], nullptr, 16 ) ;

        std::cout << std::setw(2) << i << ". hex: 0x" << D[i] << "   dec:" ;

        if( errno == ERANGE ) { std::cout << "  * range error *\n" ; errno = 0 ; }
        else std::cout << std::setw(21) << F[i] << '\n' ;
    }

    std::cout << "\n\nstd::strtoull\n-------------\n" ;
    for( int i = 0 ; i < SZ ; ++i )
    {
        F[i] = std::strtoull( D[i], nullptr, 16 ) ;

        std::cout << std::setw(2) << i << ". hex: 0x" << D[i] << "   dec:" ;

        if( errno == ERANGE ) { std::cout << "  * range error *\n" ; errno = 0 ; }
        else std::cout << std::setw(21) << F[i] << '\n' ;
    }
}

clang++ -std=c++11 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -lsupc++ main.cpp && ./a.out
std::strtoll
------------
 0. hex: 0x210743AAAEC8E34B   dec:  2379945328444498763
 1. hex: 0x7D16AE8F0E67A37E   dec:  9013583633673855870
 2. hex: 0x3A3B504AE96F6147   dec:  4196035760499941703
 3. hex: 0xA58ADFCA8EB3A217   dec:  * range error *
 4. hex: 0x649821C088CD195A   dec:  7248580711065393498
 5. hex: 0xDBDBB68209898D44   dec:  * range error *
 6. hex: 0xEC1FBB44455CD8D0   dec:  * range error *
 7. hex: 0xD2545542803C5F82   dec:  * range error *
 8. hex: 0x095720F6301B3065   dec:   673042911056310373
 9. hex: 0x7A4D0B2607D72E55   dec:  8812712303802134101
10. hex: 0xA29B364DF6469A48   dec:  * range error *


std::strtoull
-------------
 0. hex: 0x210743AAAEC8E34B   dec:  2379945328444498763
 1. hex: 0x7D16AE8F0E67A37E   dec:  9013583633673855870
 2. hex: 0x3A3B504AE96F6147   dec:  4196035760499941703
 3. hex: 0xA58ADFCA8EB3A217   dec: -6518151449594846697
 4. hex: 0x649821C088CD195A   dec:  7248580711065393498
 5. hex: 0xDBDBB68209898D44   dec: -2604287289881686716
 6. hex: 0xEC1FBB44455CD8D0   dec: -1432220254584645424
 7. hex: 0xD2545542803C5F82   dec: -3290911683593347198
 8. hex: 0x095720F6301B3065   dec:   673042911056310373
 9. hex: 0x7A4D0B2607D72E55   dec:  8812712303802134101
10. hex: 0xA29B364DF6469A48   dec: -6729725509702870456

http://coliru.stacked-crooked.com/a/7a2cdfa16bccbdef
Hi, Thanks for making me think!
I change to unsigned long long int , range will go from 0 to +18446744073709551615 (FFFFFFFFFFFFFFFF) and use strtoull --> http://www.cplusplus.com/reference/cstdlib/strtoull/

Works, but when vector start with 0.. this zero will be drop !
like vector F[8].

Literal D[8] = 095720F6301B3065, after change Int F[8] = 95720F6301B3065

[ ] ´s Pomodoro

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
// Array literal to Array Int

#include <string.h>
#include <iostream>
#include <stdlib.h> 


char *D[11] = 
{
"210743AAAEC8E34B",
"7D16AE8F0E67A37E",
"3A3B504AE96F6147",
"A58ADFCA8EB3A217",
"649821C088CD195A",
"DBDBB68209898D44",
"EC1FBB44455CD8D0",
"D2545542803C5F82",
"095720F6301B3065",
"7A4D0B2607D72E55",
"A29B364DF6469A48"
};

unsigned long long int F[11];

// char str_1[16];

main( )

{
	int i;
	
	for  (i = 0; i < 11; ++i) 
   	{
		printf("%s\n",D[i]);
	}

	printf("\n\n");

	for  (i = 0; i < 11; ++i) 
   	{
	    F[i] = strtoull (D[i], NULL, 16);
		printf("%llX \n",F[i]);
	}
 
 	printf("\n\n");
 
 return(0);
}



Last edited on
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
#include <cstring>
#include <cstdlib>
#include <cstdio>

const int SZ = 11 ;

const char* D[SZ] = // ****** must be const char* ******
                    // ****** MUST BE const char* ******
                    // ****** must be const char* ******
                    // ****** MUST BE const char* ******
{
    "210743AAAEC8E34B",
    "7D16AE8F0E67A37E",
    "3A3B504AE96F6147",
    "A58ADFCA8EB3A217",
    "649821C088CD195A",
    "DBDBB68209898D44",
    "EC1FBB44455CD8D0",
    "D2545542803C5F82",
    "095720F6301B3065",
    "7A4D0B2607D72E55",
    "A29B364DF6469A48"
};

unsigned long long F[SZ] = {} ; //long long int F[11];

int main() // ****** int main() ******
{
    for( int i = 0 ; i < SZ ; ++i )
    {
        F[i] = std::strtoull( D[i], nullptr, 16 ) ;
        std::printf( "%2d. %016llx\n", i, F[i] ) ;
    }
}

http://coliru.stacked-crooked.com/a/7c503b3fc0d348d8
Last edited on
Just to say thanks !
Topic archived. No new replies allowed.