LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2005, 09:07 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Data type conversion in C


Hello again,

I have a problem with data type conversions. I have a variable

unsigned int ipv4;

I pull information out of a MySQL database, which is stored as int(11) unsigned. You'd think they would be compatible. I retrieve a record from the table and include the column that will be stored in ipv4. It is the first column, so gets read as row[ 0 ]. In the database table, I see that the number is stored as 2479501685, which is correct. row[ 0 ] is a pointer to char, however, and when I try this:

ipv4 = atoi( row[ 0 ] );

then ipv4 comes out as 2147483647.

Actually, row[ 0 ] is one element of a MYSQL_ROW type, which, according to the MySQL C API, is "currently implemented as an array of counted byte strings". So I am assuming that one element of row can be treated as char*, which has always worked in the past.

I looked for a function like atoi() specifically for string-to-unsigned-int but found nothing.

What am I missing? Shouldn't atoi() successfully convert string to unsigned int?
 
Old 09-09-2005, 11:14 PM   #2
axial
LQ Newbie
 
Registered: Sep 2005
Location: Monroe, WI
Distribution: Slackware, Bluewhite64
Posts: 11

Rep: Reputation: 0
thats because atoi in stdlib.h is:

extern int atoi (__const char *__nptr)

not unsigned...
 
Old 09-09-2005, 11:15 PM   #3
axial
LQ Newbie
 
Registered: Sep 2005
Location: Monroe, WI
Distribution: Slackware, Bluewhite64
Posts: 11

Rep: Reputation: 0
might want to try atol
 
Old 09-10-2005, 03:29 PM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
So the issue is that atoi() returns an int instead of an unsigned int. I will try atol() as soon as I get a chance.

But there is no function specifically that returns an unsigned int? Or is this left as an exercise for the reader?
 
Old 09-10-2005, 03:41 PM   #5
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
try sprintf() or std::stringstream if you're using C++ to work with MySQL C Api.
 
Old 09-10-2005, 05:29 PM   #6
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Hi,

Maybe I wasn't clear. I am string to convert a string to an unsigned int, so sprintf() isn't going to work.

I have tried the C functions atoi() and atol() and neither of them are working correctly. The number is being stored in the mysql table as int(11) unsigned and shows up as 2479501685. When I fetch the row with that as the first field, I use

ipv4 = atoi( row[ 0 ] ); // also tried ipv4 = atol( row[ 0 ] );

but the result is coming out as 2147483647 instead of 2147483647.

Other ideas? I cannot believe that there is no way in C to convert a string to an unsigned int.

Thanks again for any ideas.
 
Old 09-10-2005, 05:47 PM   #7
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
Here's what I what was looking for:

ipv4 = strtoul( row[ 0 ], NULL, 10 );

After that, ipv4 = 2479501685.

Whee! Thanks for the help and ideas, folks!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
strange data type in c... HELP alaios Programming 9 09-03-2005 11:40 PM
c + type conversion dilberim82 Programming 8 03-08-2005 12:17 PM
conversion to non-const reference type max2878 Linux - General 2 05-23-2002 01:25 PM
a question on type conversion in C++ ReverseLogic Programming 3 11-27-2001 11:06 PM
Data Type 9 bob Linux - Newbie 1 02-10-2001 08:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration