* Following codes is to change byte[] to integer-typed-value and vice versa.
=> Note : JavaVM uses Big-Endian. And, size of each integer-type is byte(1), short(2), int(4), long(8).

public static final int
byteArrayToLongBE(byte[] b) {
    return ((b[0]&0xff) << 56) + ((b[1]&0xff) << 48)
         + ((b[2]&0xff) << 40) + ((b[3]&0xff) << 32)
         + ((b[4]&0xff) << 24) + ((b[5]&0xff) << 16)
         + ((b[6]&0xff) << 8)  + (b[7]&0xff);
}

public static final int
byteArrayToLongLE(byte[] b) {
    return (b[0]&0xff          + ((b[1]&0xff) << 8)
         + ((b[2]&0xff) << 16) + ((b[3]&0xff) << 24)
         + ((b[4]&0xff) << 32) + ((b[5]&0xff) << 40)
         + ((b[6]&0xff) << 48) + ((b[7]&0xff) << 56));
}

public static final int
byteArrayToIntBE(byte[] b) {
    return ((b[0]&0xff) << 24) + ((b[1]&0xff) << 16)
         + ((b[2]&0xff) << 8) + (b[3]&0xff);
}

public static final int
byteArrayToIntLE(byte[] b) {
    return (b[0]&0xff) + ((b[1]&0xff) << 8)
         + ((b[2]&0xff) << 16) + ((b[3]&0xff) << 24);
}

public static final short
byteArrayToShortBE(byte[] b) {
    return (short) (((b[0]&0xff) << 8) + (b[1]&0xff));
}

public static final short
byteArrayToShortLE(byte[] b) {
    return (short) ((b[0]&0xff) + ((b[1]&0xff) << 8));
}

public static final byte[]
longToByteArrayBE(long v) {
    return new byte[] {
            (byte)(v >>> 56), (byte)(v >>> 48),
            (byte)(v >>> 40), (byte)(v >>> 32),
            (byte)(v >>> 24), (byte)(v >>> 16),
            (byte)(v >>> 8),  (byte)v};
}

public static final byte[]
longToByteArrayLE(long v) {
    return new byte[] {
            (byte)v,          (byte)(v >>> 8),
            (byte)(v >>> 16), (byte)(v >>> 24),
            (byte)(v >>> 32), (byte)(v >>> 40),
            (byte)(v >>> 48), (byte)(v >>> 56)};
}

public static final byte[]
intToByteArrayBE(int v) {
    return new byte[] {
            (byte)(v >>> 24), (byte)(v >>> 16),
            (byte)(v >>> 8),  (byte)v};
}

public static final byte[]
intToByteArrayLE(int v) {
    return new byte[] {
            (byte)v,          (byte)(v >>> 8),
            (byte)(v >>> 16), (byte)(v >>> 24)};
}

public static final byte[]
shortToByteArrayBE(short v) {
    return new byte[] {(byte)(v >>> 8), (byte)v };
}

public static final byte[]
shortToByteArrayLE(short v) {
    return new byte[] {(byte)v, (byte)(v >>> 8) };
}

* Run process

// Recommanded
// Run shell command and return it's output as string
public static String
runCmd(String... cmd) {
    String r = "";
    try {
        // execute command
        ProcessBuilder pb = new ProcessBuilder(cmds);
        // pb.redirectErrorStream(true); // if needed.
        // wait till running is done.
        Process pr = pb.start();
        pr.waitFor() ;
        String line;
        // Reader output of sub process.
        BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())) ;
        // Make output String object
        while ( null != (line = br.readLine()) ) r += line + "\n";
    } catch (Exception e) {
        ; // Exception Handling!
    }
    return r;
}

// OR
public static String
    runCmd(String[] cmds) {
    String r = "";
    try {
        // execute command
        Process pr = Runtime.getRuntime().exec(cmds) ;
        // wait till running is done.
        pr.waitFor() ;
        // Reader output of sub process.
        BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())) ;
        // Make output String object
        String  line;
        while ( null != (line = br.readLine()) ) r += line + "\n";
    } catch (Exception e) {
        ; // Exception Handling!
    }
    return r;
}

* Run shell command

// We should invoke SHELL (not process command directly)
// See above for 'runCmd'
String cmd = "ls -al | grep text";
runCmd("/bin/bash", "-c", cmd);
// or : runCmd(new String[]{"/bin/bash", "-c", cmd});

* Multi-lined JLabel : We can do this by using HTML directly.

JLabel jl = new JLabel("<html>1st line<br>2nd line</html>");

* Loading java property

Properties prop = new Properties();
try {
    prop.load(new FileInputStream(property_file_path));
} catch (IOException e) {
    ; // exception handling
}

* String to Integer

try {
    int v = Integer.parseInt("3456");
} catch (NumberFormatException e) {
    ; // exception handling
}

=== to be continued...

'Language > Java' 카테고리의 다른 글

[Java][Test Code] ThreadPoolExecutor...  (0) 2013.09.25
[Java] Visibility에서 추가했으면 하는 것....  (0) 2011.04.21
[Java] Some notable stuffs of VM  (0) 2011.01.06
[Java] Package private in Java  (0) 2009.11.16
[Java][Tips] Compile...  (0) 2009.10.18
[[ blog 이사 과정에서 정확한 posting날짜가 분실됨. 년도와 분기 정도는 맞지 않을까? ]]

#define STORE_LE16(bUFF,nUM) ( ((bUFF)[1] = (UINT8) ((nUM)>>8)), \

                              ((bUFF)[0] = (UINT8) (nUM)) )

#define STORE_LE32(bUFF,nUM) ( ((bUFF)[3] = (UINT8) ((nUM)>>24)),    \
                              ((bUFF)[2] = (UINT8) ((nUM)>>16)),     \
                              ((bUFF)[1] = (UINT8) ((nUM)>>8)),      \
                              ((bUFF)[0] = (UINT8) (nUM)) )

/* Store value into a bUFFer in Big Endian format */
#define STORE_BE16(bUFF,nUM) ( ((bUFF)[0] = (UINT8) ((nUM)>>8)),     \
                              ((bUFF)[1] = (UINT8) (nUM)) )

#define STORE_BE32(bUFF,nUM) ( ((bUFF)[0] = (UINT8) ((nUM)>>24)),    \
                              ((bUFF)[1] = (UINT8) ((nUM)>>16)),     \
                              ((bUFF)[2] = (UINT8) ((nUM)>>8)),      \
                              ((bUFF)[3] = (UINT8) (nUM)) )

/* Little Endian to Host integer format conversion macros */
#define LEtoHost16(ptr)  (U16)( ((U16) *((U8*)(ptr)+1) << 8) | \
                                ((U16) *((U8*)(ptr))) )

#define LEtoHost32(ptr)  (U32)( ((U32) *((U8*)(ptr)+3) << 24) | \
                                ((U32) *((U8*)(ptr)+2) << 16) | \
                                ((U32) *((U8*)(ptr)+1) << 8)  | \
                                ((U32) *((U8*)(ptr))) )

/* Big Endian to Host integer format conversion macros */
#define BEtoHost16(ptr)  (U16)( ((U16) *((U8*)(ptr)) << 8) | \
                                ((U16) *((U8*)(ptr)+1)) )

#define BEtoHost32(ptr)  (U32)( ((U32) *((U8*)(ptr)) << 24)   | \
                                ((U32) *((U8*)(ptr)+1) << 16) | \
                                ((U32) *((U8*)(ptr)+2) << 8)  | \
                                ((U32) *((U8*)(ptr)+3)) )

+ Recent posts