When we query rows by a BLOB column we must convert the BLOB to its String hex form - Java java.lang

Java examples for java.lang:Hex

Description

When we query rows by a BLOB column we must convert the BLOB to its String hex form

Demo Code


//package com.java2s;

public class Main {
    final protected static char[] hexArray = "0123456789ABCDEF"
            .toCharArray();//from  www .  jav  a  2  s.  c  om

    /**
     * When we query rows by a BLOB column we must
     * convert the BLOB to its String hex form
     * see:
     * http://www.sqlite.org/lang_expr.html#litvalue
     */
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        String rawHex = new String(hexChars);
        return rawHex;
        //        String blobLiteral = "X'" + rawHex + "'";
        //        return blobLiteral;
    }
}

Related Tutorials