GIDForums

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2003, 03:00
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

[function] Converting Bytes


PHP Code:

<?php

/**
   NAME        : convert_from_bytes()
   VERSION     : 1.0
   AUTHOR      : J de Silva
   DESCRIPTION : returns FLOAT value; handles converting
                 bytes to values in units between
                 'bits' and 'gigabytes'.
   TYPE        : function
   ======================================*/

function convert_from_bytes( $bytes, $to=NULL )
{
  $float = floatval( $bytes );
  switch( $to )
  {
    case 'Kb' :            // Kilobit
      $float = ( $float*8 ) / 1024;
      break;
    case 'b' :             // bit
      $float *= 8;
      break;
    case 'GB' :            // Gigabyte
      $float /= 1024;
    case 'MB' :            // Megabyte
      $float /= 1024;
    case 'KB' :            // Kilobyte
      $float /= 1024;
    default :              // byte
  }
  unset( $bytes, $to );
  return( $float );
}
?>


EXAMPLE CODE
PHP Code:

<?php
// let's say we have a dummy byte size to convert
$filesize = 3670016;  // bytes


// if we need to convert this bytes to KB (kilobytes)
$KB = convert_from_bytes( $filesize, 'KB' );
  // $KB == 3584

// if we need to convert these bytes to MB (megabytes)
$MB = convert_from_bytes( $filesize, 'MB' );
  // $MB == 3.5

// if we need to convert these bytes to b (bits)
$b = convert_from_bytes( $filesize, 'b' );
  // $b == 29360128
?>

  #2  
Old 11-Jan-2007, 03:08
Stripe-man Stripe-man is offline
New Member
 
Join Date: Jan 2007
Posts: 2
Stripe-man is on a distinguished road

Re: [function] Converting Bytes


um... the math is incorrect. this really should be :

Code:
function convert_from_bytes( $bytes, $to=NULL ) { $float = floatval( $bytes ); switch( $to ) { case 'Kb' : // Kilobit $float = ( $float*8 ) / 1024; break; case 'b' : // bit $float *= 8; break; case 'GB' : // Gigabyte $float /= pow(2,30); case 'MB' : // Megabyte $float /= pow(2,20); case 'KB' : // Kilobyte $float /= pow(2,10);; default : // byte } unset( $bytes, $to ); return( $float ); } ?>
  #3  
Old 11-Jan-2007, 04:45
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 726
admin will become famous soon enough

Re: [function] Converting Bytes


Thank you for your feedback. However, there is nothing wrong with the 'math'.

You didn't notice that the last three 'case's are without breaks.

Because you missed that little detail, your suggestion does not work. It would, if you had placed the break between the cases.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #4  
Old 11-Jan-2007, 05:05
Stripe-man Stripe-man is offline
New Member
 
Join Date: Jan 2007
Posts: 2
Stripe-man is on a distinguished road

Re: [function] Converting Bytes


Hum... I see...
My fault because i had actually copied your code.. I thought letter for letter on a different screen while looking at your code example..

You are right i did miss the LACK of the breaks.

Are there any advantages of doing this either way ?
  #5  
Old 11-Jan-2007, 05:12
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 726
admin will become famous soon enough

Re: [function] Converting Bytes


I've been dabbling in PHP long enough to admit that there is always a better way to write code. Especially something I wrote 3 years ago... :P

Your idea is good and I see nothing wrong with it (except for the missing breaks), so we'll leave it here with our comments so far.

The reader can decide for herself how she would use the information here.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
 

Recent GIDBlogMaster?s Degree by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[function] Show limited words of a string / text JdS PHP Code Library 0 05-Jun-2003 09:25
[Function] Int to Money Conversion Elmseeker PHP Code Library 1 30-Jan-2003 23:10
[function] BBCodes Collection JdS PHP Code Library 1 26-Jan-2003 09:48
[function] Validate Email Addresses I JdS PHP Code Library 0 23-Jan-2003 03:21
[function] Timer JdS PHP Code Library 0 18-Jan-2003 18:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.