A conversion prgram

Started by
6 comments, last by Servant of the Lord 11 years, 6 months ago
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
Advertisement
Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.

Break the string into a series chars, C_str should do fine. Then cast every character to an unsigned int.

No need, as you can use [font=courier new,courier,monospace]operator[][/font] to index characters in the string.

@OP: I'd use std::transform with std::toupper to convert the string to upper case, then I'd use std::remove_if 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 std::remove_if with std::isalpha to get it to just alpha characters, and then use std::transform with a function that did std::toupper() - '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 std::ostream, 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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

[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.

No need, as you can use [font=courier new,courier,monospace]operator[][/font] to index characters in the string.

@OP: I'd use std::transform with std::toupper to convert the string to upper case, then I'd use std::remove_if 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 std::remove_if with std::isalpha to get it to just alpha characters, and then use std::transform with a function that did std::toupper() - '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 std::ostream, 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.
There are allot of standard functions to convert data types. stringstream, itoa, atoi, etc..
But you could also write your own conversions.. wink.png

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


Offtopic
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. smile.png
Offtopic

~EngineProgrammer

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

strings are just a collection of numbers organized in a specific pattern such that each letter corrosponds to a particular number(see ASCII), 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.


There are allot of standard functions to convert data types. stringstream, itoa, atoi, etc..
But you could also write your own conversions.. wink.png

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


Offtopic
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. smile.png
Offtopic

~EngineProgrammer


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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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.
f@dzhttp://festini.device-zero.de
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.


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

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:
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

. . . .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

. . . .output a ? to the result, or discard that char
otherwise
. . . . Subtract 96, so 'a' becomes '1', and so on.

This topic is closed to new replies.

Advertisement