G
G
gleendo2017-01-14 14:21:56
Java
gleendo, 2017-01-14 14:21:56

How to understand the moment in converting a number to a binary string?

I analyze an example from a lecture on Java.
There is an example: "We encode a codepoint that fits into a single-byte UTF8"
There is a code example:

import java.nio.charset.StandardCharsets;

public class App {
    public static void main(String[] args) {
        int codePoint = 0b110011;
        String cp = leftZeroPadding(Integer.toBinaryString(codePoint), 8);

        System.out.println("codePoint: " + cp);

        String str = new String(new int[] {codePoint}, 0, 1);
        byte[] utf8bytes = str.getBytes(StandardCharsets.UTF_8);

        System.out.println("utf8bytes: " + leftZeroPadding(Integer.toBinaryString(0xFF & utf8bytes[0]), 8));
    }

    public static String leftZeroPadding(String str, int length) {
        while (str.length() < length) {
            str = "0" + str;
        }

        return str;
    }
}

Everything is clear, but I don’t understand why in the next line, in the toBinaryString () method, 0xFF is written? What is this for?
System.out.println("utf8bytes: " + leftZeroPadding(Integer.toBinaryString(0xFF & utf8bytes[0]), 8));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2017-01-14
@evgeniy8705

utf8bytes[0] - is of type byte
, values ​​of the primitive type byte range from -128 to 127.
The Integer.toBinaryString(...) method converts an int to a binary string.
Now look:
let's say utf8bytes[0] = 0xFE, at first glance 0xFE= 254, but for the byte type 0xFE=-2.
If this 0xFE (of type byte) is converted to type int, then we will also get -2, but for type int it will already be 0xFFFFFFFE.
so they do it like this: 0xFF & utf8bytes[0]
0xFF in this expression is of type int and is an abbreviation for 0x000000FF
utf8bytes[0] here, suppose it is 0xFE and when converted to int will be 0xFFFFFFFE
The result of the expression is 0x000000FE - which corresponds to 254, and when converted in bin is "11111110"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question