C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 10th, 2003, 02:54 AM
aleezah aleezah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 15 aleezah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
strint to unsigned int conversion!!

hi!!
i want to convert a string to int..
but i guess the value is bit too long to be stored as int....so i want to store it as unsigned int...but cant figure out how to make this conversion..
#include <stdio.h>
#include <string.h>
int main()
{
int inbytes1;
char string[50]="265695678";

printf("string %s\n", string);
inbytes1 = atoi(string);
printf("int: %d\n", inbytes1);
}
regards

Reply With Quote
  #2  
Old May 10th, 2003, 10:25 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
I'm assuming that you're using a 16-bit compiler like Turbo-C. If this is the case, you can use atol() and convert it to a long value instead.

Reply With Quote
  #3  
Old May 10th, 2003, 10:43 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 28 m 3 sec
Reputation Power: 1974
Because the integer types have a fixed number of bits, they have fixed ranges. The number of bits assigned to an int is system dependent, which means that it could have a different number of bits on different systems. It looks like a short int is fixed at 16 bits and a long int at 32 bits, but I'm not sure that's really the case.

Since most of our work is with a 16-bit compiler, I'm used to an int being 16 bits long, such that a signed int's range is from –32,768 to 32,767 and an unsigned int is from 0 to 65,535. As you can see, your number is still beyond the range of the unsigned int. A long int (32-bit) ranges +/- 2.14 billion (+/- 2 G) or unsigned from 0 to 4.29 billion (4 G). Your number of 265 million will fit in a long int.

The long int version of atoi() is atol(), which returns a long int value. I didn't find a atoul, but there is also a family of strtoul(), etc, functions which appear to do the same thing as atoi & atol, but I'm not familiar with them and don't know how they differ.

Both atoi et al. and strtoi et al. appear to handle overflow the same way (trying to convert a number that exceeds the range of the data type); from the description of atoi:
Quote:
RETURN VALUES
The atoi() function returns the converted integer value.

If the converted value overflows, LONG_MIN or LONG_MAX is returned (according to the sign of the value) and errno is set to ERANGE. If no character could be converted, zero is returned.

Read the man/help page for the function you end up using for details.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > strint to unsigned int conversion!!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap