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 11-24-2010, 01:13 AM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
[C program] convert gigabyte to byte


I'm learning C and trying to write a program to convert gigabyte into byte. It will take 50% of the value given and then convert that value into byte. But the problem is the output goes into minus / negative value. Like, if I enter 22, I get output: -2147483648.

Code:
#include <stdio.h>
#include <math.h>

main (int argc, char* argv[])
{
   int gig;
   int byte;
   printf ("Enter gigabyte: ");
   scanf ("%d", &gig);
   byte = (gig*0.5)*1024*1024*1024;
   printf ("Byte size is: %d\n", byte);
   return 0;
}
Thanks.

Last edited by noir911; 11-24-2010 at 01:14 AM.
 
Old 11-24-2010, 02:41 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Use 'unsigned int' instead of 'int'. You may need to use 'unsigned long int' if numbers get really large. Make sure to printf "%u" for unsigned and "%l" for u long int.

http://www.cplusplus.com/reference/c...cstdio/printf/

Also, why 50% ? That doesn't really convert GB to bytes does it ?

Last edited by H_TeXMeX_H; 11-24-2010 at 02:44 AM.
 
Old 11-24-2010, 03:12 AM   #3
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Thanks!

I'm now getting positive value. But the output I'm getting is still not right.

eg. 50% of 22GB converted in bytes is 11811160064. But my program is giving me 3221225472.

Code:
#include <stdio.h>
#include <math.h>

main (int argc, char* argv[])
{
   unsigned int gig;
   unsigned int byte;
   printf ("Enter gigabyte: ");
   scanf ("%u", &gig);
   byte = (gig*0.5)*1024*1024*1024;
   printf ("Byte size is: %u\n", byte);
   return 0;
}
Thanks.

Also, I'm taking 50% off as a part of the learning process.
 
Old 11-24-2010, 03:37 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Yes, it is caused by integer overflow, do this:

Code:
#include <stdio.h>

int main (void)
{
   unsigned int gig;
   printf ("Enter gigabyte: ");
   scanf ("%u", &gig);
   printf ("Byte size is: %lu\n", (long)1024*1024*1024*gig/2);
   return 0;
}
 
Old 11-24-2010, 07:06 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by H_TeXMeX_H View Post
Use 'unsigned int' instead of 'int'. You may need to use 'unsigned long int' if numbers get really large.
On a 32 bit x86 architecture, you would need 'unsigned long long'.

On a 32 bit x86 architecture, 'unsigned long int' is still the same size as 'unsigned int'

Quote:
Originally Posted by noir911 View Post
convert gigabyte into byte.
You should be aware that "gigabyte" is often used to mean 10 to the ninth power, rather than the meaning you seem to intend: 2 to the thirtieth power. Many people observe the convention that GiB unambiguously means 2 to the thirtieth power bytes.

Quote:
It will take 50% of the value given and then convert that value into byte.
Was it luck or understanding that made you multiply by .5 rather than divide by 2?

Dividing by 2 would leave an integer result, so 23/2 would be 11 just like 22/2. Also an integer result would have just 32 bits when multiplied by 1024*1024*1024, so even if that result were stored in an unsigned long long, it would have already been truncated.

Multiplying an int by .5 creates a temporary of type double, which can be multiplied by 1024 three times with no truncation. Then all the truncation occurred when you stored that double into a 32 bit integer. That truncation would be avoided if you had stored into a long long.

Quote:
printf ("Byte size is: %d\n", byte);
But then printing a long long via printf is tricky. In fact I don't remember how you can do that.

You could use some ugly kludge such as
Code:
unsigned long long byte;
...
printf("Byte size is: %u%09u\n",
   (unsigned)( byte/1000000000 ),
   (unsigned)( byte%1000000000 ) );
Edit: GCC (at least version 3.4.6 that I tried) seems to support %lld for printing long long. Previous C compilers I have used didn't. So all you needed was:

Code:
unsigned long long byte;
...
printf("Byte size is: %lld\n", byte);

Last edited by johnsfine; 11-24-2010 at 07:27 AM.
 
Old 11-24-2010, 07:37 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I would use '%llu', because it is unsigned.

I did forget that int does differ between 32-bit and 64-bit, which is unfortunate. I was going to recommend using inttypes.h to just request a nice large type, but it may just complicate things.

Either way, my example does work, I have tested it (on 64-bit, but it should work on 32-bit too).
 
  


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
[SOLVED] How to convert negative integer to byte array? Basel Programming 5 10-27-2010 12:35 PM
Serial Port communication program always lose one byte data: 0x00 leon.zcom Programming 2 12-14-2009 07:00 PM
Needed: Byte-Swapping Program tonyfreeman Linux - Software 2 06-20-2007 06:16 PM
How I can Convert 4 byte IP to an IP string of form ("0.0.0.0") touqeer.ansar Programming 2 07-16-2006 11:02 PM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM

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

All times are GMT -5. The time now is 07:50 AM.

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