'java equalent of HEXTORAW Function In Oracle

Hi I want the java equalent of HEXTORAW Function In Oracle .in oracle you can use :

SELECT hextoraw(to_char(ascii('z'))) from DUAL; 

that generates 0122 or use

SELECT hextoraw(to_char(ascii('C'))) from DUAL; 

which generates 67 all methods that i found in java dont add left 0 to the ascii codes above 99 how can i solve this? thease are 2 of methods that i used:

 String key="zs01C";
        IntStream intStream = key.codePoints();
        String rawKey = intStream.mapToObj(c->Integer.toString(c)).collect(Collectors.joining());
        System.out.println("Method1: "+rawKey);
        intStream = key.codePoints();

        String rawKey2 = intStream.mapToObj(c->String.format("%2s", c).replace(' ', '0')).collect(Collectors.joining());
        System.out.println("Method2: "+rawKey2);


Solution 1:[1]

Hex string usually double the size of byte array, so you can try this

String hex = "BC0800AF";
byte[] raw = new byte[hex.length()/2];
for(int i=0;i<hex.length()/2;i+=2) {
   char c = hex.charAt(i);
   char c1 = hex.charAt(i+1);
   raw[i] = (byte)((c << 8) | c1);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dickens A S