Search code examples
javaio

String to bytearray conversion


public static void main(String args[]){


        try{
            FileInputStream fs = new FileInputStream("test.txt");
            int i = fs.read();
            while(i != -1){
                System.out.println( i);
                i = fs.read();
            }
        }catch(Exception e){
            System.out.println(e);
        }
    }

The test.txt file contains " Č " . The output is 196 140 . the UTF-16 value of Č is 268 . Can you please help me why the answer is 196 and 140


Solution

  • If you don't specify a specific encoding, Java uses "UTF-8" character encoding as default.

    The UTF-8 (hex) for Č is 0xC4 0x8C, which translates to decimal as 196 and 140 respectively. When using UTF-16 for encoding, Č is represented by hex value of 0x010C, which in conversion to decimal becomes 268.