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 }
  • 相关阅读:
    向TRichEdit插入图片的单元
    等待程序结束后, 自动启动程序的批处理
    执行程序函数
    基于IWICImage的截图代码
    线程中WICImage与Bitmap数据转换
    清理win10过期补丁的命令
    [转] 常见的哈希函数
    豆瓣小组爬虫.....^_^
    如何在ASP.NET Core中实现CORS跨域
    在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证
  • 原文地址:https://www.cnblogs.com/lovychen/p/3601716.html
Copyright © 2011-2022 走看看