how to parse non string data
-
Hello, Hope it is not a stupid question but suppose I have an int i = 20031225 and I would like to parse it to get the 4 first figures (2003) and the last 2 figures (25), like the functions CStringT::Left and CStringT::Right. is it possible to do it without too much coding and without converting into string? Thnks.
-
Hello, Hope it is not a stupid question but suppose I have an int i = 20031225 and I would like to parse it to get the 4 first figures (2003) and the last 2 figures (25), like the functions CStringT::Left and CStringT::Right. is it possible to do it without too much coding and without converting into string? Thnks.
Arris7 wrote:
I would like to parse it to get the 4 first figures (2003)...
Divide by 10000. Why are you against using an intermediary string?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Arris7 wrote:
I would like to parse it to get the 4 first figures (2003)...
Divide by 10000. Why are you against using an intermediary string?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Dividing by 10000 is a very good idea. And to get the latest 2 figures by doing i%100 works fine also. Actually I use a huge amount of data and it takes too much time to process. I also would like to get any numbers from the figure. for example I would like to extract 01 from 20030112. Is it doable?
-
Dividing by 10000 is a very good idea. And to get the latest 2 figures by doing i%100 works fine also. Actually I use a huge amount of data and it takes too much time to process. I also would like to get any numbers from the figure. for example I would like to extract 01 from 20030112. Is it doable?
Of course, you can do:
extraction = (i / 100) % 100;
but, as already stated by DavidCrow, this is better done using strings. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Of course, you can do:
extraction = (i / 100) % 100;
but, as already stated by DavidCrow, this is better done using strings. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
CPallini wrote:
but, as already stated by DavidCrow, this is better done using strings.
I never said one way was any better than the other. I was simply asking why he was adamantly against the string solution. It would take quite a bit of empirical testing to find which of the two methods was the "better" one. One might be faster, but the other could be more intuitive.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Of course, you can do:
extraction = (i / 100) % 100;
but, as already stated by DavidCrow, this is better done using strings. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
thanks for the solution. but why using string is better? In my case I use a huge volume of data of int type and my data is fixed length. data extracted with the solutions you provided me will be used for calculations. So converting to string + parsing data + reconverting again to int will probably be expensive in term of execution speed.
-
CPallini wrote:
but, as already stated by DavidCrow, this is better done using strings.
I never said one way was any better than the other. I was simply asking why he was adamantly against the string solution. It would take quite a bit of empirical testing to find which of the two methods was the "better" one. One might be faster, but the other could be more intuitive.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
thanks for the solution. but why using string is better? In my case I use a huge volume of data of int type and my data is fixed length. data extracted with the solutions you provided me will be used for calculations. So converting to string + parsing data + reconverting again to int will probably be expensive in term of execution speed.
Arris7 wrote:
So converting to string + parsing data + reconverting again to int will probably be expensive in term of execution speed
I don't know. Maybe it depends on what you are exactly doing... :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.