Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-05-2003, 08:56 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
convert a string to a int , and back

how can i convert a string, to an int, and back to a string?
sde is offline   Reply With Quote
Old 02-05-2003, 11:26 AM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
***there was an implementation error in my previous post. edited to work properly***
this code also assumes that the size of long and int are the same. this is architecture and compiler dependant and not always true. additional checks need to be done if not.

Code:
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
  /* some definitions */
  char *str = "12345";
  char *p;
  long num;
  char s[12]; /* '-2147483647\0' (this is the longest decimal string from a 32bit signed int */

  /* convert a string to an int */
/*  
DONT USE atoi(), it does not detect errors
  num = atoi(str);
*/
  errno = 0;
  num = strtol(str, &p, 0);
  if(p != '\0')
  {
    printf("error: invalid string\n");
    return 1;
  }
  if(errno == ERANGE && (num == LONG_MIN || num == LONG_MAX))
  {
    printf("error: number outside range of signed long int\n");
    return 1;
  }

  /* convert an int to a string */
  snprintf(s, sizeof(s), "%d", (int)num);

  printf ("the string is '%s'\n", s);

  return 0;
}
joe_bruin is offline   Reply With Quote
Old 02-05-2003, 11:46 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
thanks a lot! ... also, welcome to CN =)
sde is offline   Reply With Quote
Old 03-02-2003, 12:45 AM   #4 (permalink)
Neophyte
Registered User
 
Neophyte's Avatar
 
Join Date: Mar 2003
Posts: 1
Neophyte is on a distinguished road
If you're using C++, you can do it like this:

Code:
#include <string>
#include <sstream>
#include <iostream>
using namespace std;

int main(void) {
  string str = "12345";
  int num;
  string conv;

  /* convert str to int */
  istringstream iss ( str );
  iss >> num;
  cout << "num: " << num << endl;

  /* convert num to string */
  ostringstream oss;
  oss << num;
  istringstream iss2 ( oss.str() );
  iss2 >> conv;
  cout << "conv: " << conv << endl;

}
Neophyte is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
A request from Ali regarding Back OS processes redhead Platform/API C++ 0 08-11-2004 01:48 AM


All times are GMT -8. The time now is 12:02 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting