user4442168
user4442168

Reputation:

how does parseLong method work in java?

in below code i cant understand how "CAFEBABE" string convert to Numbers ?

class string {
public static void main (String [] args) {
    long l = Long.parseLong( "CAFEBABE" ,16);
    System.out.println(l); }
} // 3405691582

can anybody tell me how this happening ? thanks

Upvotes: 1

Views: 218

Answers (2)

Ivan Matavulj
Ivan Matavulj

Reputation: 145

OxCAFEBABE = E * 16^0 + B * 16^1 + A * 16^2 + B * 16^3 + E * 16^4 + F * 16^5 + A * 16^6 + C * 16^7  (decimal)

In the above calculation replace hexadecimal digits with their decimal representations, i.e. A with 10, B with 11, C with 12, E with 14 and F with 15

Upvotes: 3

Eran
Eran

Reputation: 393851

CAFEBABE is a valid Hexadecimal number, so it can be parsed as a hexadecimal number, which is what the 16 argument means.

Upvotes: 1

Related Questions