显示给定字符的ASCII值

时间:2014-09-10 03:34:18

标签: java ascii

我写了一个基于Java web的程序来显示给定字符的ASCII值。但事实并非如此 好好工作。请帮助我。这是我的代码。无需在数据库中添加字符。

主豆

public mainbean() {}

private String charTest;

public String getCharTest() {
    return charTest;
}

public void setCharTest(String charTest) {
    this.charTest = charTest;
}

public String DoDisplay() throws IOException {

    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));

   //  System.out.println("Enter the char:");
    // String str = buff.readLine();
    String str = buff.readLine();

     // for ( int i = 0; i < str.length(); ++i){
    int i = 0;
    char c = str.charAt(i);
    int j = (int) c;
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "ASCII OF" + c + "=" + j, ""));
    return null;
}

这是我的xhtml页面

<h:head>
    <title>Complain System</title>
</h:head>

<h:body>
    <h:form id="form">
        <h:outputText value="name"/>
        <p:inputText value="#{mainbean.charTest}"/>
        <p:commandButton value="submit" ajax="false" action="#{mainbean.doDisplay()}"/>
    </h:form>
</h:body>

这是Ctrl页面

public class charCtrl {

    private EntityManager em;

    public static void add() throws IOException {
        BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
        String str = buff.readLine();
        int i = 0;
        char c = str.charAt(i);
        int j = (int) c;
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "ASCII OF" + c + "=" + j, ""));
    }
}

2 个答案:

答案 0 :(得分:1)

你可以试试 1.)String.valueOf(ch).codePointAt(0); 2.)使用int

转换字符

答案 1 :(得分:0)

您可以将char标记为int。所以:

int asciiVal = (int) yourChar;

示例代码:

char yourChar = 'a';
int asciiVal = (int) yourChar;
System.out.println("ASCII for " + yourChar + " is " + asciiVal);