Java中的radix参数是什么,它是如何工作的? [英] What is the radix parameter in Java, and how does it work?

查看:176
本文介绍了Java中的radix参数是什么,它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解函数 Integer.parseInt()的基数是将字符串转换为的基础。 11基数10不能用基数/基数16转换为 B 而不是 17

I understand that radix for the function Integer.parseInt() is the base to convert the string into. Shouldn't 11 base 10 converted with a radix/base 16 be a B instead of 17?

以下代码根据教科书打印17:

The following code prints 17 according to the textbook:

public class Test {
  public static void main(String[] args) {
    System.out.println( Integer.parseInt("11", 16) );
  }
}


推荐答案

使用基数执行 ParseInt 操作时,11基数16被解析为17,这是一个简单的值。然后将其打印为基数10.

When you perform the ParseInt operation with the radix, the 11 base 16 is parsed as 17, which is a simple value. It is then printed as radix 10.

您想要:

System.out.println(Integer.toString(11, 16));

这取十进制值11(此时没有基数,就像拥有十一西瓜一样(比一个人的手指数多一个))并用基数16打印,结果是 B

This takes the decimal value 11(not having a base at the moment, like having "eleven" watermelons(one more than the number of fingers a person has)) and prints it with radix 16, resulting in B.

当我们取一个 int 值时,它将作为基数2存储在计算机的物理内存中(几乎在所有情况下),但这是无关紧要的,因为解析和tostring转换适用于任意基数(默认为10)。

When we take an int value it's stored as base 2 within the computer's physical memory (in nearly all cases) but this is irrelevant since the parse and tostring conversions work with an arbitrary radix (10 by default).

这篇关于Java中的radix参数是什么,它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆