简体   繁体   中英

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

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.

Try creating InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"), StandardCharsets.UTF_16);

Or provide some other suitable charset encoding value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM