英文到中文的转换

就是java中怎么将汉字转换成16进制

3个回答

import java.util.ArrayList;
import java.util.List;

public class ThritySix {
/**

  • @param args
    */
    public static void main(String[] args) {
    String str = "中华人发共和国";
    System.out.println(show(str,16));
    }

    public static List show(String source, int p) {
    List list = new ArrayList();
    char[] arr = source.toCharArray();
    for (char c : arr) {
    StringBuilder sb = new StringBuilder();
    int tt = c;

    int ii = 0;
    //System.out.println(tt);

    while (tt != 0) {
    ii = tt % p;
    if (ii > 9) {//如果大于9则用A-Z代替
    sb.insert(0, getData(ii));//插入到最前面
    } else {
    sb.insert(0, ii);//插入到最前面
    }
    tt = tt / p;
    }
    list.add(sb.toString());
    }

    return list;
    }
    public static String getData(int i) {
    switch (i) {
    case 10:
    return "A";
    case 11:
    return "B";
    case 12:
    return "C";
    case 13:
    return "D";
    case 14:
    return "E";
    case 15:
    return "F";
    case 16:
    return "G";
    case 17:
    return "H";
    case 18:
    return "I";
    case 19:
    return "J";
    case 20:
    return "K";
    case 21:
    return "L";
    case 22:
    return "M";
    case 23:
    return "N";
    case 24:
    return "O";
    case 25:
    return "P";
    case 26:
    return "Q";
    case 27:
    return "R";
    case 28:
    return "S";
    case 29:
    return "T";
    case 30:
    return "U";
    case 31:
    return "V";
    case 32:
    return "W";
    case 33:
    return "X";
    case 34:
    return "Y";
    case 35:
    return "Z";

    }
    return "";
    }

}

补充说明:
[code="java"]public static List show(String source, int p)
[/code]
p是多少进制,[code="java"]
case 17:
return "H";
case 18:
return "I";
case 19:
return "J";
case 20:
return "K";
...[/code]
你只要十六进制,这些可以不要

[code="java"]
当然还有最简单的方法:
public String[] show(String source,int p){ String[] tem = new String[source.length()]; char[] arr=source.toCharArray(); int i=0; for(char c:arr){ tem[i++]=Integer.toString(c,p); } return tem; }

[/code]

朋友,问题要自动关闭啦,结分哦

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问