dec方法java,Java:如何将dec转换为32bit int?

How to convert decimal presentation of an ip address to 32bit integer value in java?

I use InetAddress class and getLocalHost method to obtain an IP adress:

public class getIp {

public static void main(String[] args) {

InetAddress ipaddress;

try {

ipaddress=InetAddress.getLocalHost();

System.out.println(ipaddress);

}

catch(UnknownHostException ex)

{

System.out.println(ex.toString());

}

}

}

Than I should convert the result to 32bit integer value and than to string, how do I do that?

Thanks!

解决方案

If the IP address is IPv6, it won’t work.

Otherwise for the Sun/Oracle implementation and IPv4, you can play dirty:

ipaddress.hashCode()—works but may break in the future, therefore not recommended.

Otherwise (recommended): int ipv4 = ByteBuffer.wrap(addr.getAddress()).getInt()

以下是将十进制变量k转换为二进制并输出的DOSBox汇编语言程序,假设k为WORD类型的变量,存放在bx寄存器中: ``` .MODEL SMALL .STACK 100H .DATA k DW 1234H ; 假设k的十进制值为1234 buffer DB 16 DUP(0) ; 存储二进制结果的缓冲区 .CODE MOV AX, @DATA ; 初始化数据段寄存器 MOV DS, AX MOV CX, 16 ; 循环计数器,变量k为16位 MOV BX, 8000H ; 用于按位与的掩码 LEA SI, buffer ; 缓冲区指针 loop_start: AND BX, k ; 按位与,取出最高位 CMP BX, 0 ; 判断最高位是否为0 JZ zero_bit ; 如果为0则跳到zero_bit标签 MOV BYTE PTR [SI], '1' ; 将'1'存储到缓冲区中 JMP next_bit ; 跳到next_bit标签 zero_bit: MOV BYTE PTR [SI], '0' ; 将'0'存储到缓冲区中 next_bit: SHR BX, 1 ; 将掩码右移一位 INC SI ; 缓冲区指针加一 DEC CX ; 计数器减一 JNZ loop_start ; 如果计数器不为零则跳到loop_start标签 MOV AH, 9 ; 调用DOS中断21H,输出字符串 LEA DX, buffer ; 字符串指针 INT 21H MOV AH, 4CH ; 调用DOS中断21H,退出程序 INT 21H END ``` 上述程序通过按位与掩码、移位、累加和条件跳等指令,将十进制变量k转换为二进制,并存储到一个指定的内存区域中。程序中的注释已经解释了每个指令的作用和意义,如果需要进一步了解汇编语言的相关知识,可以参考相关的书籍和教程。请注意,本程序使用了DOSBox模拟器来运行,如果您需要在其他环境中运行,可能需要进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值