简体   繁体   English

显示给定字符的ASCII值

[英]Display an ASCII value of a given character

I wrote a Java web based program to display ASCII value of given character. 我编写了一个基于Java Web的程序来显示给定字符的ASCII值。 but it is not working properly. 但它不能正常工作。 pls help me. 请帮助我。 this is my code. 这是我的代码。 no need to add characters to the database. 无需向数据库添加字符。

main bean 主豆

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;
}

this is my xhtml page 这是我的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>

This is Ctrl page 这是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, ""));
    }
}

you can try 1.) String.valueOf(ch).codePointAt(0); 您可以尝试1.)String.valueOf(ch).codePointAt(0); 2.) cast the character with int 2.)用int转换字符

You can typecast your char as an int . 您可以将char转换为int So: 所以:

int asciiVal = (int) yourChar;

Sample code: 样例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM