Jump to content

  • Log In with Google      Sign In   
  • Create Account

A conversion prgram


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Braeden   Members   -  Reputation: 110

Like
0Likes
Like

Posted 26 September 2012 - 08:57 PM

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

Sponsor:

#2 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 26 September 2012 - 09:11 PM

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

#3 Cornstalks   Crossbones+   -  Reputation: 6995

Like
3Likes
Like

Posted 26 September 2012 - 11:45 PM

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

#4 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 27 September 2012 - 02:10 AM


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


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

Guess I should stop thinking like a C programmer.

#5 EngineProgrammer   Members   -  Reputation: 295

Like
0Likes
Like

Posted 27 September 2012 - 06:40 PM

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

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. Posted Image
Offtopic

~EngineProgrammer

#6 slicer4ever   Crossbones+   -  Reputation: 3906

Like
2Likes
Like

Posted 27 September 2012 - 08:51 PM

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

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

Edited by slicer4ever, 27 September 2012 - 08:51 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#7 Trienco   Crossbones+   -  Reputation: 2195

Like
1Likes
Like

Posted 27 September 2012 - 10:07 PM

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

#8 Servant of the Lord   Crossbones+   -  Reputation: 19665

Like
0Likes
Like

Posted 27 September 2012 - 10:36 PM

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.

Edited by Servant of the Lord, 27 September 2012 - 10:36 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS