zoukankan      html  css  js  c++  java
  • Java 之进制转换

    //十进制转十六进制

    1 import java.util.Scanner;
    2 public class Main{
    3 public static void main(String[] args){
    4 Scanner input = new Scanner(System.in);
    5 int n;
    6 n=input.nextInt();
    7 System.out.println(Integer.toHexString(n).toUpperCase());//转换成大写字母;toLowerCase():转换成小写的16进制;
    8   }
    9 }

     //各种进制转换

     1 import java.util.Scanner;
     2 public class Main {
     3     public static void main(String[] args) {
     4         Scanner sc=new Scanner(System.in);
     5        String c=sc.next();
     6        System.out.println(Integer.toHexString(c).toUpperCase());//十进制转换成十六进制
     7         System.out.println(Integer.toBinaryString(c).toUpperCase());//十进制转换成2进制
     8         System.out.println(Integer.toOctalString(c).toUpperCase());//十进制转换成八进制
     9        System.out.println(Integer.valueOf(c,16).toString());//十六进制转换成十进制
    10        String t=Integer.valueOf(c, 16).toString();
    11        int n=Integer.parseInt(t);//先将十六进制转换成字符串然后将字符串转换成整数
    12        System.out.println(Integer.toOctalString(n).toUpperCase());//整数转换成八进制
    13     }
    14 }

    //十六进制转八进制(太长的java不行了)用c

     1 #include <stdio.h>
     2 #include<cstring>
     3 int x2i(char c)
     4 {
     5     if (c >= 'A')
     6         return c - 55;
     7     else
     8         return c - 48;
     9 }
    10 
    11 int main()
    12 {
    13     int i, j, n;
    14     char a[10][100001];
    15     scanf("%d", &n);
    16     for (i = 0; i < n; i++)
    17         scanf("%s", a[i]);
    18     for (i = 0; i < n; i++)
    19     {
    20         char* p = a[i];
    21         int len = strlen(p);
    22         if (len % 3 == 1)
    23         {
    24             printf("%o", x2i(p[0]));
    25             j = 1;
    26         }
    27         else if (len % 3 == 2)
    28         {
    29             printf("%o", x2i(p[0])*16+x2i(p[1]));
    30             j = 2;
    31         }
    32         else
    33         {
    34             printf("%o", x2i(p[0])*256+x2i(p[1])*16+x2i(p[2]));
    35             j = 3;
    36         }
    37         for (; j < len; j += 3)
    38             printf("%04o", x2i(p[j])*256+x2i(p[j+1])*16+x2i(p[j+2]));
    39         printf("
    ");
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    刷新页面的问题
    JS属性
    1-4_基本概念_进程间通信及同步
    1-3_基本概念_程序&进程
    1-2_基本概念_用户和组&目录、链接及文件
    1-1_基本概念_内核&shell
    d 操作系统概述
    对比生成测试算法用例
    简单的排序算法:冒泡排序法
    简单的排序算法:插入排序法
  • 原文地址:https://www.cnblogs.com/lovychen/p/3601716.html
Copyright © 2011-2022 走看看