繁体   English   中英

Jave-方法是-parseByte(String s,int radix)-我听不懂

[英]Jave - method is - parseByte(String s, int radix) - I can't understand it

这是本教程给我的一个例子。 我无法理解答案-解析字节值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为底的数字。由于我们只有10个数字(0,..,9)符号,因此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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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