Total Pageviews

Monday, November 21, 2011

HEX mode in VIM

  • Enter hex edit mode:
    %!xxd
  • Exit hex edit mode:
    %!xxd -r

Show Java Console on Mac OS

Applications -> Utitlities -> Java Preferences:


Wednesday, November 16, 2011

Multiples of bytes

Multiples of bytes
SI decimal prefixes IEC binary prefixes
Name
(Symbol)
Value Name
(Symbol)
Value
kilobyte (kB) 103 kibibyte (KiB) 210 = 1.024 × 103
megabyte (MB) 106 mebibyte (MiB) 220 ≈ 1.049 × 106
gigabyte (GB) 109 gibibyte (GiB) 230 ≈ 1.074 × 109
terabyte (TB) 1012 tebibyte (TiB) 240 ≈ 1.100 × 1012
petabyte (PB) 1015 pebibyte (PiB) 250 ≈ 1.126 × 1015
exabyte (EB) 1018 exbibyte (EiB) 260 ≈ 1.153 × 1018
zettabyte (ZB) 1021 zebibyte (ZiB) 270 ≈ 1.181 × 1021
yottabyte (YB) 1024 yobibyte (YiB) 280 ≈ 1.209 × 1024

Convert byte size into human readable format in Java ( and GWT )

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));
    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
Java GWT:
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));
 String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1)
   + (si ? "" : "i");
 return toFixed(bytes / Math.pow(unit, exp), 1) + " " + pre
    + "B";
}
public static native String toFixed(double d, int fixed)/*-{
  return d.toFixed(fixed);
}-*/;

Orgin from here

JSNI function in GWT to format numbers

public class NumberUtil {
 
 public static native String toFixed(double d, int fixed)/*-{
  return d.toFixed(fixed);
 }-*/;
 
 public static native String toPrecision(double d, int precision)/*-{
  return d.toPrecision(precision);
 }-*/;

}

see also this article.

Format numbers in javascript

var num = 1;
var result = num.toFixed(2); // result = 1.00

num = 123.4567;
result = num.toFixed(3); // result = 123.456

num = 123.4567;
result = num.toPrecision(4); // result = 123.4

num = 123.4567;
result = num.toPrecision(3); // result = 123

num = 555.55;
result = num.toPrecision(2); // result will equal 5.6e+2

see also this article.

Sunday, November 13, 2011

DATE and TIME in TCL script using clock

TCL: clock command

GWT: JSNI function to validate URL

public native boolean isValidUrl(String url) /*-{
    var pattern = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    return pattern.test(url);
}-*/;

Java: use signed types to store unsigned values

short, int, long are always signed in Java. However, it only matters if you are doing math with it. Otherwise, you can store unsigned values using the signed types by utilizing the sign bit.

If you do need to do some math:

1) for unsigned short, us,

convert it to int by masking: int i = us & 0xffff;

Do all arithmetic with int operands, modulo 0xffff.

When you are done, cast the result back to int and transmit it.

2) for unsigned int, ui,

convert it to long by masking: long l = ui & 0xffffffffL;

Do all arithmetic with long operands, modulo 0xffffffffL.

When you are done, cast the result back to int and transmit it.