how to convert str to arr[]?

string str="1B 42 E8 1B 43 40 0B 10 00 90 01 B4 00 2C 01 00 00 78 01 01";
array unsinged a[];

and I want to store each 2 data in hexadecimal as one element in array.
such as a[0]=1B,a[1]=42,a[2]=E8.....etc.

so I can change these hex into dec .

who can give some code or fuction name?
thanks in advance.
You might find std::istringstream useful for this:

http://www.cplusplus.com/reference/iostream/istringstream/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <sstream> // std::istringstream
#include <iomanip> // std::hex

// ... stuff ...

	std::string str =
		"1B 42 E8 1B 43 40 0B 10 00 90 01 B4 00 2C 01 00 00 78 01 01";

	// convert string into input stream
	std::istringstream iss(str);

	unsigned v;
	iss >> std::hex >> v; // read one number (in hex) from string


When reading from an input stream using >> you can specify the input base using std::hex for hexidecimal.
Last edited on
thank you Galik,
"unsigned v" waht kind of variable of v?
int or char?
can it be array?
unsigned is unsigned int
can it be array?

Of course: unsigned array[100];
I still can't get right answer.

I hope some one can help me.
Post your code so far.
see below
Last edited on
data deleted.
Last edited on
Please use code tags when posting code: http://www.cplusplus.com/forum/articles/16853/

I notice that line 34 was doing something strange and crashing the program. Its seems to run when you comment it out:

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <gman/bug.h>

#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
//#include <stdio.h>
#include <ctime> // use this for C++ rather than <time.h>

using namespace std;

void strallcut(char *str)
{
	bug_func();
	int i, j = 0;
	char sp[512];
	for(i = 0; *(str + i) != '\0'; i++)
	{
		if(*(str + i) == ' ')
			continue;
		sp[j++] = *(str + i);
	}
	sp[j] = '\0';//0same?
	strcpy(str, sp);
}
std::string& trim(std::string &s, const char* str)
{
	bug_func();
	if(s.empty())
		return s;
	s.erase(0, s.find_first_not_of(str));
	s.erase(s.find_last_not_of(str) + 1);
	//s.erase(47); // What is this for?
	return s;
}
void purehex()
{
	bug_func();
	ifstream ifs("sft001.txt");
	ofstream ofs("sft002.txt");
	string strTmp,s;
	while(getline(ifs, strTmp))
	{
		trim(strTmp, " ");
		if(!strTmp.empty()&&'/'!= strTmp.at(0))
		if(string::npos!=strTmp.find("Request")||string::npos!= strTmp.find("Answer")||string::npos!= strTmp.find("Port"))
				continue;
		strTmp += "\n";
		s += strTmp;
	}
	s.erase(0, 1);
	ofs << s;
	ifs.close();
	ofs.close();
}
void combin()
{
	bug_func();
	string s, strT;
	ifstream ifs("sft002.txt");
	ofstream ofs("sft003.txt");
	char strline[256];
	while(ifs.getline(strline, 256))
	{
		strallcut(strline);
		if(strline[0] == '0' && strline[1] == '2' || strline[0] == '0'
			&& strline[1] == '3')
		{
			s += "\n";
			s += strline;
		}
		else
			s += strline;
	}
	s.erase(0, 1);
	ofs << s;
	ifs.close();
	ofs.close();

}
void delesc()
{
	bug_func();

	string s, str;
	ifstream ifs("sft003.txt");
	ofstream ofs("sft004.txt");
	while(getline(ifs, str))
	{
		int k = 0, len = str.length() / 2;
		for(int i = 0; i < len; i++)
		{
			if(str[i * 2] == '1' && str[2 * i + 1] == 'B')//delete 1B
			{
				str[2 * i] = str[2 * i + 2] - 4;
				str[2 * i + 1] = str[2 * i + 3];
				str.erase(2 * i + 2, 2);
			}
		}
		s += str;
		s += "\n";
	}
	ifs.close();
	ofs << s;
	ofs.close();
}

int main(int argc, char* argv[])
{
	bug_func();
	time_t time1, time2;
	time(&time1);

	purehex();
	cout << "purehex finished" << endl;
	combin();
	cout << "combin finished" << endl;
	delesc();
	cout << "desc finished" << endl;

	time(&time2);
	cout << time2 - time1 << "total time" << endl;

	system("pause");
	return 0;
}
Last edited on
//s.erase(47); // What is this for?


it is not strange.

for the content
1
2
3
4
5
6
7
8
Request: 2010-6-19 11:14:13.67164 (+0.0313 seconds)

02 1B 5B 01 16 E8 1B 43 40 01 10 00 E2 05 00 00 ..[..è.C@...a...
D2 1B 44 00 00 2E 16 00 00 5A 00 00 00 E7 ò.D......Z...? 

Answer: 2010-6-19 11:14:13.68764 (+0.0156 seconds)

03 1B 5B 01 16 00 00 01 40 10 00 E2 05 00 00 D2 ..[.....@..a...ò

in line 3,4,8
the character after 47 is ASCII code.and I want to delete them.I only need the hex data.
thank you for your reply.

did you compile my coed? and did it do something to sft001.txt?

as you can see,all my code to do is snatch some userful data from a txt file.and get rid of

empty cell ,empty line and ASCII code.and at last I will put these continuous hex number into

array(I still don't know how to do it effecient)
1
2
3
4
\Program Files\DEV-CPP\main.cpp C:\Program Files\DEV-CPP\C gman/bug.h: No such file or directory. 

15 C:\Program Files\DEV-CPP\main.cpp `bug_func' undeclared (first use this function) 
 

hi,Galik
I compiled and it gives above message.I think mabe your develope circumstance is different .
Hi peripheral,

Sorry I forgot I left my debug trace in the code. You can just delete those bug_func() macros.

What exactly is it that you trying to extract from this data?

1
2
3
4
5
6
7
8
Request: 2010-6-19 11:14:13.67164 (+0.0313 seconds)

02 1B 5B 01 16 E8 1B 43 40 01 10 00 E2 05 00 00 ..[..è.C@...a...
D2 1B 44 00 00 2E 16 00 00 5A 00 00 00 E7 ò.D......Z...? 

Answer: 2010-6-19 11:14:13.68764 (+0.0156 seconds)

03 1B 5B 01 16 00 00 01 40 10 00 E2 05 00 00 D2 ..[.....@..a...ò
The problem with this is on lines that are not long enough. You could just test the length:
1
2
3
4
	if(s.length() > 47)
	{
		s.erase(47); // What is this for?
	}
Last edited on
Galik,I have compiled success!
than you very much!
I at last get data as
1
2
3
4
5
031B0105000001401000E2050000D20400002E1600005A0000001E
021B0106E803400B1000900115006400B4005C01030000000000A4
031B010600000B401000900100006400B4005C010400020000005E
021B0107E803400B1000900101106B0000100E000780A06A011006
031B010700000B401000900100006B0000100E0004800300011037

and I am try to do next work.
to arrange the data into array.
for example the first line above
a[0]=03 a[1]=1B a[2]=01.........a[last]=1E

do you have some good fuctions suugest ?
by the way I am very appreciated for your help.
When you say a[0]=03 do you mean a is an integer or a string?
sorry I forget to say.

at first I definea[0]=03 a[1]=1B...as unsigned int.

so I want to use atoi(),convert hex to decimal but when I input "1B"it doesn't work,

then I have to use strtol();and in the function

"long int strtol(const char *nptr, char **endptr, int base)"

the first parameter is a string.so I must define a[0]=03 a[1]=1B...as string

do you think I am right?
but strol() can only execute one data one time.

I mean it can't operate strtol(a[0],null,16) neither strol(a[i],null,16)

how can I do?

and strol() can't deal with unsigned char value.

how can I do?
Well I would read it in one line at a time and extract each pair as a substring. You could then convert each substring pair to a unsigned integer and put it in your array:

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
#include <sstream>
#include <fstream>
#include <iostream>

typedef unsigned int uint;

int main()
{
	std::ifstream ifs("input.txt");

	// read each line
	std::string line;
	while(std::getline(ifs, line))
	{
		// every 2 chars in string is one array entry
		uint items = line.length() / 2;

		// create array
		uint* data = new uint[items];

		for(uint i = 0; i < items; ++i)
		{
			// extract each hex pair as a substring
			std::string val = line.substr(i * 2, 2);

			// convert hex string into unsigned int (uint)
			// and store in the array
			std::istringstream(val) >> std::hex >> data[i];
		}

		// process array
		for(uint i = 0; i < items; ++i)
		{
			std::cout << data[i] << ' ';
		}
		std::cout << '\n';

		// free memory for array
		delete[] data;
	}
}
Last edited on
thank you Galik ,your code is very usefull to me.is more simple than my thought.
Topic archived. No new replies allowed.