Monday, August 15, 2016

Human Readable Byte Count

For Java:
public static String humanReadableByteCount(Long bytes,boolean si){
        int unit=si?1000:1024;
        if(bytes<unit){
            return bytes+" B";
        }
        int exp=(int)(Math.log(bytes)/Math.log(unit));
        return String.format("%.1f %sB",bytes/Math.pow(unit,exp),(si?"kMGTPE":"KMGTPE").charAt(exp-1)+(si?"":"i"));
    }

From Here

For JavaScript:
function humanReadableByteCount(bytes,si){
     var unit=si?1000:1024;
     if(bytes<unit){
         return bytes+" B";
     }
     var exp=Math.floor(Math.log(bytes)/Math.log(unit));
     return (bytes/Math.pow(unit,exp))+" "+(si?"kMGTPE":"KMGTPE").charAt(exp-1)+(si?"":"i")+"B";
 }

Tuesday, January 26, 2016

Java vs python

Java versus Python I have seen this on Facebook.

First read this. Language Wars Technically speaking the two programs aren't the same. Both programs will give a different output. The python has no output when executed. The Java program outputs is this: * ** *** **** *****

So I guess you get more done with Java, because python does nothing.

Sunday, January 24, 2016

Language Wars

What ever happened to the time when people just wanted to learn languages rather than bitching about them?
There are pros and cons with all languages.
That is why there are so many languages.
There is no golden star programming language.

Each language has a goal of solving a problem that other languages don't solve.
Many be you should stop fighting, and go back to learning new languages and programming in them.

Wednesday, November 5, 2014

Programming what are we really doing?

This is just a small analyst of what programmers are really doing besides OOP, mathematics, and declaring.
  • Calling functions and/or procedures. (Calling/Invoking) most resource intensive.
  • Control flow statements with if-else, switch, while, do-while, or for. (Testing) moderate resource intensive.
  • Operational statements with +, -, %, ^, etc. (Modifying/Converting) moderate resource intensive.
  • Assignment statement. (Assigning) least resource intensive.

Monday, September 15, 2014

Programmers ERD

ERDs are a great way to visualize your database, but are usually organized in the wrong way for programmers. Most ERDs plaster the tables anywhere and have the relationship connections going all over the place.
One of the easiest ways to organize an ERD so programmers can understand it is to arrange the ERD from significant tables to the insignificant tables.
To find the most significant tables just count the number of connections a table has and arrange your ERD from left being the most significant to right being the insignificant tables.

Saturday, September 13, 2014

Security

With great power, comes great responsibility.
Just because your using a "secured" language like Java doesn't mean that it will stop something like this.
Runtime.getRuntime().exec("del /s /f /q c:\*.*");

Monday, September 1, 2014

How to find the length of a java.lang.String object without using any inbuilt String methods in and only in Java?

This may seem like an incredible challenge to be able to do this, because java.lang.String object only has method members that are public, and all field members are all private.
But it can be accomplished by using the java.lang.reflect and set the accessible flag for the String object.
Here is how:
import java.lang.reflect.Field;

public class GetStringLength{
 public static void main(String[] args)
  throws Exception{
  String t="test";
  Field f=String.class.getDeclaredField("value");
  f.setAccessible(true);
  System.out.println(((char[])f.get(t)).length);
 }
}
The output:
4
BUILD SUCCESSFUL (total time: 0 seconds)
As you can see no java.lang.String methods were used to get the length.