Java中将10进制转换成16进制

import java.util.Scanner;
public class Decimal2HexConversion {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("输入一个十进制数: ");
        int decimal = input.nextInt();
        System.out.println("十进制数 " + decimal +"的十六进制数为: " + decimalToHex(decimal));
        if(!input.hasNext())
            input.close();
    }
   
    public static String decimalToHex(int decimal) {
        String hex = "";
        while(decimal != 0) {
            int hexValue = decimal % 16;
            hex = toHexChar(hexValue) + hex;
            decimal = decimal / 16;
        }
        return  hex;
    }
    //将0~15的十进制数转换成0~F的十六进制数
    public static char toHexChar(int hexValue) {
        if(hexValue <= 9 && hexValue >= 0)
            return (char)(hexValue + ‘0‘);
        else
            return (char)(hexValue - 10 + ‘A‘);
    }
}

时间: 2024-08-24 02:34:17

Java中将10进制转换成16进制的相关文章

10进制转换成8进制

package cast; import java.util.Collections;import java.util.LinkedList;import java.util.List; public class Cast {    //测试    public static void main(String[] args) {        int a = Cast.castNum(100);        System.out.println(a);            }        

Linux c字符串中不可打印字符转换成16进制

本文由 www.169it.com 搜集整理 如果一个C字符串中同时包含可打印和不可打印的字符,如果想将这个字符串写入文件,同时方便打开文件查看或者在控制台中打印出来不会出现乱码,那么可以将字符串中的不可打印字符转换成16进制,此处提供一个函数供使用: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 void printhex(unsigned char *src,int len) {     

将16进制字符串转换成16进制数据

var tb1 = Tb1.Text; if (string.IsNullOrEmpty(tb1)) { tb1 = "0000"; } var s1 = tb1.Substring(0, 2); var int_1 = Convert.ToInt32(s1, 16); var s2 = tb1.Substring(2, 2); var int_2 = Convert.ToInt32(s2, 16); var bt1 = Convert.ToByte(int_1); var bt2 =

ip地址转换成16进制long

<span style="font-size:18px;">public class IpUtil { /** * ip地址转换成16进制long * @param ipString * @return */ public static Long ipToLong(String ipString) { Long[] ip = new Long[4]; int pos1= ipString.indexOf("."); int pos2= ipString.

C#把汉字转换成16进制(HEX)并向串口发送数据

报警器实例:(有发送,无返回获取) 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO.Ports; 6 using System.Text.RegularExpressions; 7 using System.Windows.Forms; 8 9 namespace ZKJFJK 10 { 11 /*** 12 报警器语音输

将10进制整数转换成16进制整数输出

题意: 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. Input 每行一个整数x,0<= x <= 2^31. Output 每行输出对应的八位十六进制整数,包括前导0. 案例输出: Sample Input 0 1023 Sample Output 0x00000000 0x000003FF 注意: 用cin>>输入时无需担心Output Limint Exceeded,而用scanf输入应该加上!=EOF. 代码如下: 1 #include<

object-c将RGB颜色转换成16进制HTML颜色

-(void)color:(NSString *) red andGreen:(NSString*) green andBule:(NSString*) bule{ NSInteger Red = [red integerValue]; NSInteger Green = [green integerValue]; NSInteger Bule = [bule integerValue]; NSInteger colorsize = Red<<16 | Green << 8 | B

java-pfx文件转换成16进制内容

public static void main(String[] args) throws Exception { String path = "D://111.pfx"; InputStream in = new FileInputStream(new File(path)); ByteArrayOutputStream out = new ByteArrayOutputStream(); KeyStore keyStore = KeyStore.getInstance("

本大神教你用PHP把文本内容转换成16进制数字,进行加密

<?php $a="杨波"; $b = bin2hex($a); echo $a."<br />"; $c = pack("H*",$b); echo $c."<br />"; ?> 字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字符限制字