Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Converting from string to int

Name: Anonymous 2010-10-16 11:18

I've been trying to figure out for 2 hours how to translate a series of chars (in a string) into int variables in C++. What I got this far is:

int x << (int)stringvar[5];

It builds, but it returns every character as a 0.

anyone got any ideas?

Name: Anonymous 2010-10-16 11:20

bump

Name: Anonymous 2010-10-16 11:21

Hmm, well, after detailed analysis of this line of what appears to be program code, I think you're doing something incorrectly.

Name: Anonymous 2010-10-16 11:22

Noko, w'the fuck's he?

Name: Anonymous 2010-10-16 11:24

0/10, also int x << stringvar

Name: Anonymous 2010-10-16 11:42

>>5
ITYM man atoi.

Name: Anonymous 2010-10-16 11:47

>>5
You need the cast to integer to left-shift x. Also the int should have parentheses around it or be removed.

Name: Anonymous 2010-10-16 11:52

>>6
What is this man atoi you are talking about?
Why are you talking about men?

Name: Anonymous 2010-10-16 12:06

>>6
I thought about atoi for a moment, but was disgusted with this thought.  atoi is inherently dirty, C-like, .h-file including filth.  Learn to use C++ toolset when writing C++.

Name: Anonymous 2010-10-16 12:11

>>9
But then you tried using an int as a fucking stringstream or whatever the fuck you were doing with that bitshift you thought was overloaded.

Name: Anonymous 2010-10-16 12:47


int convertInteger(char* c) {
  int i = 0;
  int r = 0;
  while (c[i] != '\0') {
    r = r * 10;
    switch (c[1]) {
      case "0":
        r = r + 0;
        break;
      case "1":
        r = r + 1;
        break;
      case "2":
        r = r + 2;
        break;
      case "3":
        r = r + 3;
        break;
      case "4":
        r = r + 4;
        break;
      case "5":
        r = r + 5;
        break;
      case "6":
        r = r + 6;
        break;
      case "7":
        r = r + 7;
        break;
      case "8":
        r = r + 8;
        break;
      case "9":
        r = r + 9;
        break;
      default:
        return NULL;
    }
  }
  return r;
}

Name: Anonymous 2010-10-16 12:51

another solution

int convertInteger(char* c) {
  int i = 0;
  int r = 0;
  while (c[i] != '\0') {
    r = r * 10;
    switch (c[1]) {
      case "9": ++r;
      case "8": ++r;
      case "7": ++r;
      case "6": ++r;
      case "5": ++r;
      case "4": ++r;
      case "3": ++r;
      case "2": ++r;
      case "1": ++r;
      case "0": break;
      default: return NULL;
    }
  }
  return r;
}

Name: Anonymous 2010-10-16 12:57


97     /* parses 's' as a 'base' base string */
98     int64_t parsestring(char *s, int base) {
99    
100     unsigned int i,j,slen=strlen(s),n=0;
101     int64_t r = 0;
102    
103     for(i=0;i<slen;i++) {
104     if(s[i]>='0' && s[i]<='9')
105     n = s[i] - 48;
106     else if(s[i]>='A' && s[i]<='F')
107     n = s[i] - 55;
108     else if(s[i]>='a' && s[i]<='f')
109     n = s[i] - 87;
110     else
111     return -1;
112     if(n>=base)
113     return -1;
114     for(j=1;j<slen-i;j++)
115     n*=base;
116     r+=n;
117     }
118     return r;
119     }

Name: Anonymous 2010-10-16 12:58

>>11,12
[b][i]U MENA c[i] AND [code]case '9':[code][/i][/b]

[spoiler][sup]or just replace the entire thing with [code]if (c[i] >= '0' && c[i] <= '9') r += c[i] - '0';[/code][/sup][/spoiler]

Name: Anonymous 2010-10-16 13:00


#import <sstream>

int main() {
  int myint;
  std::stringstream waffen_ss;
  waffen_ss << "1234";
  waffen_ss >> myint;
}

Name: Anonymous 2010-10-16 13:09

>>10
I couldn't care to check if there was an appropriate procedure defined with this puny ad-hoc polymorphism (so-called overloading) for >>1's case (I thought that there was, but if there was not, I couldn't care less).

I detest C++.  I detest it so unconditionally.  I crave for ability to abhor C++ to death.

Name: Anonymous 2010-10-16 13:25

>>15
Oops, I didn't read >>1 closely enough.

#import <sstream>
using std::stringstream;

int main() {
  string stringvar("12345");
  int *array = new int[stringvar.size()];
  for (int i=0; i < stringvar.size(); i++) {
    stringstream waffen_ss(stringvar.substr(i, 1));
    waffen_ss >> array[i];
  }
}

Name: Anonymous 2010-10-16 19:13


yhbt :: String -> Int
yhbt = read

Name: Anonymous 2010-12-23 23:59

Name: Anonymous 2013-08-25 20:49

but how to check if conversion fails?

Name: Anonymous 2013-08-25 21:33

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
If you don't care about ``evil C'' use strtol. It tells you if conversion fails by setting errno.

Name: Anonymous 2013-08-25 21:49

The language of the machine posesses no type, it is pure.

Name: Anonymous 2013-08-25 21:56

>>22
http://en.wikipedia.org/wiki/Tagged_architecture
Read it and weep, Intel kike. No more ``butt fart overflows'' for you.

Name: Anonymous 2013-08-25 22:09

>>23
What cause will promote a mote apond my eye, some long dead architecture, nay should take a bit further than that methinks.

Don't change these.
Name: Email:
Entire Thread Thread List