Java Jave-methodis-parseByte(字符串s,int基数)-我可以';我不明白

Java Jave-methodis-parseByte(字符串s,int基数)-我可以';我不明白,java,Java,这是教程给我的一个例子。我无法理解答案-123的解析字节值是83,-1a的解析字节值是-26。请试着用一种非常简单的方式向我解释这个方法 import java.lang.*; public class ByteDemo { public static void main(String[] args) { // create 2 byte primitives bt1, bt2 byte bt1, bt2; // create and assig

这是教程给我的一个例子。我无法理解答案-123的解析字节值是83,-1a的解析字节值是-26。请试着用一种非常简单的方式向我解释这个方法

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create 2 byte primitives bt1, bt2
      byte bt1, bt2;

      // create and assign values to String's s1, s2
      String s1 = "123";
      String s2 = "-1a";

      // create and assign values to int r1, r2
      int r1 = 8;  // represents octal
      int r2 = 16; // represents hexadecimal

      /**
       *  static method is called using class name. Assign parseByte
       *  result on s1, s2 to bt1, bt2 using radix r1, r2
       */
      bt1 = Byte.parseByte(s1, r1);
      bt2 = Byte.parseByte(s2, r2);

      String str1 = "Parse byte value of " + s1 + " is " + bt1;
      String str2 = "Parse byte value of " + s2 + " is " + bt2;

      // print bt1, bt2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

第一个值123被解释为八进制数,即基数为8的数字

现在1*8^2+2*8+3=64+16+3=83

第二个值-1a被解释为十六进制数,即以16为基数的数字。由于数字(0、…、9)只有10个符号,因此符号a、b、c、d、e、f用于表示十进制值大于9的数字。所以a(16)=10(10),b(16)=11(10)等等


并且-(1*16+10)=-26

第一个值123被解释为八进制数,即以8为基数的数字

现在1*8^2+2*8+3=64+16+3=83

第二个值-1a被解释为十六进制数,即以16为基数的数字。由于数字(0、…、9)只有10个符号,因此符号a、b、c、d、e、f用于表示十进制值大于9的数字。所以a(16)=10(10),b(16)=11(10)等等


并且-(1*16+10)=-26

在第一个示例中,您将123解释为八进制表示的数字,这意味着第一个数字不是100而是64(8*8)。因此123被解释为8*8*1+8*2+3=83。
在第二示例中,1a被解释为十六进制表示。因此-1a=-(16*1+10)=-26。

在第一个示例中,您将123解释为八进制表示的数字,这意味着第一个数字不是100,而是64(8*8)。因此123被解释为8*8*1+8*2+3=83。 在第二示例中,1a被解释为十六进制表示。所以-1a=-(16*1+10)=-26。

doc可能帮助你doc可能帮助你