Sign in to follow this  

A conversion prgram

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to work out how to convert strings to integers, ex "Hello world" should become "7.5.12.12.15 23.15.17.12.4". How would I do this? I dont want source code, I just want a basic explanation so I can figure out exactly how to use it. Thanks guys

Share this post


Link to post
Share on other sites
[quote name='kd7tck' timestamp='1348715494' post='4984214']
Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.
[/quote]
No need, as you can use [font=courier new,courier,monospace]operator[][/font] to index characters in the string.

@OP: I'd use [url="http://www.cplusplus.com/reference/algorithm/transform/"]std::transform[/url] with [url="http://www.cplusplus.com/reference/clibrary/cctype/toupper/"]std::toupper[/url] to convert the string to upper case, then I'd use [url="http://www.cplusplus.com/reference/algorithm/remove_if/"]std::remove_if[/url] to remove all the characters that weren't between 'A' and 'Z', and then all you have to do is subtract 'A' and add 1 to each character and you're good to go. Alternatively, I'd use [url="http://www.cplusplus.com/reference/algorithm/remove_if/"]std::remove_if[/url] with [url="http://www.cplusplus.com/reference/clibrary/cctype/isalpha/"]std::isalpha[/url] to get it to just alpha characters, and then use [url="http://www.cplusplus.com/reference/algorithm/transform/"]std::transform[/url] with a function that did [url="http://www.cplusplus.com/reference/clibrary/cctype/toupper/"]std::toupper()[/url] - 'A'+ 1 in one pass. Once the string is just a collection of bytes that represent the right values for each character, I'd probably just output each character to a [url="http://www.cplusplus.com/reference/iostream/ostream/"]std::ostream[/url], casting each char to an int and adding a period between numbers.

Something like that. How I'd actually do it kind of depends on the situation.

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1348724734' post='4984235']
[quote name='kd7tck' timestamp='1348715494' post='4984214']
Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.
[/quote]
No need, as you can use [font=courier new,courier,monospace]operator[][/font] to index characters in the string.

@OP: I'd use [url="http://www.cplusplus.com/reference/algorithm/transform/"]std::transform[/url] with [url="http://www.cplusplus.com/reference/clibrary/cctype/toupper/"]std::toupper[/url] to convert the string to upper case, then I'd use [url="http://www.cplusplus.com/reference/algorithm/remove_if/"]std::remove_if[/url] to remove all the characters that weren't between 'A' and 'Z', and then all you have to do is subtract 'A' and add 1 to each character and you're good to go. Alternatively, I'd use [url="http://www.cplusplus.com/reference/algorithm/remove_if/"]std::remove_if[/url] with [url="http://www.cplusplus.com/reference/clibrary/cctype/isalpha/"]std::isalpha[/url] to get it to just alpha characters, and then use [url="http://www.cplusplus.com/reference/algorithm/transform/"]std::transform[/url] with a function that did [url="http://www.cplusplus.com/reference/clibrary/cctype/toupper/"]std::toupper()[/url] - 'A'+ 1 in one pass. Once the string is just a collection of bytes that represent the right values for each character, I'd probably just output each character to a [url="http://www.cplusplus.com/reference/iostream/ostream/"]std::ostream[/url], casting each char to an int and adding a period between numbers.

Something like that. How I'd actually do it kind of depends on the situation.
[/quote]

I didn't realize he only wanted to do uppercase letters.

Guess I should stop thinking like a C programmer.

Share this post


Link to post
Share on other sites
There are allot of standard functions to convert data types. stringstream, itoa, atoi, etc..
But you could also write your own conversions.. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

stringstream takes average 2,000,000 ms to convert int <-> string
itoa and atoi takes average 350,000 ms to convert.


[b]Offtopic[/b]
my convertions takes around 87,000ms for int to string. And around 43,000ms for string to int.

Meaning self written conversions are more effective, if good written. You are the programmer, you decide all the errorchecking.
I don't have a single check of wrong input to increase all performance. But I'm the only programmer at my projects so I know that I don't need to enter wrong input. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[b]Offtopic[/b]

~EngineProgrammer

Share this post


Link to post
Share on other sites
[quote name='Braeden' timestamp='1348714647' post='4984213']
I've been trying to work out how to convert strings to integers, ex "Hello world" should become "7.5.12.12.15 23.15.17.12.4". How would I do this? I dont want source code, I just want a basic explanation so I can figure out exactly how to use it. Thanks guys
[/quote]
strings are just a collection of numbers organized in a specific pattern such that each letter corrosponds to a particular number(see [url="http://en.wikipedia.org/wiki/ASCII"]ASCII[/url]), your implementation is already flawed, you use letter offset's, but you don't account between caps/lower case letters, so, assuming that capitals would come after your lowercase letters, than H would be 26+7. i'm not sure what convention you want to use, but I don't see why you can't just use the ASCII system already in place, however if you do need to change the number representing the character then Cornstalk's already provided a pretty solid implementation of what to do.

[quote name='EngineProgrammer' timestamp='1348792829' post='4984548']
There are allot of standard functions to convert data types. stringstream, itoa, atoi, etc..
But you could also write your own conversions.. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

stringstream takes average 2,000,000 ms to convert int <-> string
itoa and atoi takes average 350,000 ms to convert.


[b]Offtopic[/b]
my convertions takes around 87,000ms for int to string. And around 43,000ms for string to int.

Meaning self written conversions are more effective, if good written. You are the programmer, you decide all the errorchecking.
I don't have a single check of wrong input to increase all performance. But I'm the only programmer at my projects so I know that I don't need to enter wrong input. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[b]Offtopic[/b]

~EngineProgrammer
[/quote]

I truthfully hope you are either working with incredibly long strings/70's era processors, or that you simply meant micro-seconds(generally represented as 'mus' or 'us'), and not milliseconds, still, 2 seconds is a long time even for the standard library, so i'm not really sure what your doing. Edited by slicer4ever

Share this post


Link to post
Share on other sites
Maybe he forgot to mention something like "for 1 billion test runs". In either case it doesn't really matter, since it has little to nothing to do with the actual topic. It's not about converting string<->int, but essentially about subtracting one number from another (while skipping values outside a certain range and using a different offset depending on which range you're in).

At no point should stringstream, atoi, etc. have anything to do with it.

Share this post


Link to post
Share on other sites
Yeah, stringstream doesn't take 2 seconds to do a single conversion. It doesn't even take 2 millaseconds for a single conversion, so I'm not sure where that data came from, but in my own experience it's inaccurate. Yes, a hand-rolled integer conversion would be faster in most cases, so you are doing less (it's less general), but premature optimization is not good. Use hand-rolled where you need speed, otherwise don't re-invent the wheel.

[quote name='Trienco' timestamp='1348805234' post='4984589']
At no point should stringstream, atoi, etc. have anything to do with it.
[/quote]
Quoted for truth - look at Cornstalk's post. The only place int to string (not vise-versa) is needed, is for putting the results in a new string if the OP desires.

I'd do it something like this:
[i]Cast string to lowercase. (std::transform, and std::tolower)
Iterate over each char in string (just index into it)
I'd cast the char to an int
//Since ASCII lowercase letters begins at 97, and goes to 122, and space is 32...
if the int is 32[/i]
[i]. . . .[/i][i]output a '-' to the result, if desired, or leave it as a space, or discard it.
if the int is over 122 or under 97[/i]
[i]. . . .[/i][i]output a ? to the result, or discard that char
otherwise
. . . . Subtract 96, so 'a' becomes '1', and so on.[/i] Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

This topic is 1905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this