簡體   English   中英

顯示給定字符的ASCII值

[英]Display an ASCII value of a given character

我編寫了一個基於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, ""));
    }
}

您可以嘗試1.)String.valueOf(ch).codePointAt(0); 2.)用int轉換字符

您可以將char轉換為int 所以:

int asciiVal = (int) yourChar;

樣例代碼:

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