//十进制转十六进制
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("\n"); 40 } 41 return 0; 42 }