将文本转换为ascii代码

时间:2015-03-10 17:32:07

标签: java ascii

您好我很想知道如何使用我通过java读取的文本文件将该字符转换为ascii代码(例如,h = 104)。我已经有了代码,以便程序读取文本文件。该文件为: 这是第一行,这是第二行,这是第三行。每一行都有自己的路线。现在我需要一些帮助来尝试分离每个字符,然后将它们转换为ascii代码。

        package readwritetextfile;
        import java.io.*;

        public class ReadWriteTextFile {

            /**
             * @param args the command line arguments
             */

                // TODO code application logic here
                /** JDK 6 or before. */

          /**
          * Fetch the entire contents of a text file, and return it in a String.
          * This style of implementation does not throw Exceptions to the caller.
          *
          * @param aFile is a file which already exists and can be read.
          */

            /**
             * Fetch the entire contents of a text file, and return it in a String.This style of implementation does not throw Exceptions to the caller.
             * @param aArguments
             * @param aFile is a file which already exists and can be read.
             * @return
             * @throws java.io.IOException
             */
          public static void main (String... aArguments) throws IOException {
            File testFile;
              testFile = new File("C:\\...\\readlines.txt");
            System.out.println("Original file contents: " + getContents(testFile));
            setContents(testFile, "The content of this file has been overwritten...");
            System.out.println("New file contents: " + getContents(testFile));

            /**
             *
             * @param aFile
             * @return
             */}
            static public String getContents(File aFile) {
            //...checks on aFile are elided
            StringBuilder contents = new StringBuilder();

            try {
              //use buffering, reading one line at a time
              //FileReader always assumes default encoding is OK!
              BufferedReader input =  new BufferedReader(new FileReader(aFile));
              try {
                String line = null; //not declared within while loop
                /*
                * readLine is a bit quirky :
                * it returns the content of a line MINUS the newline.
                * it returns null only for the END of the stream.
                * it returns an empty String if two newlines appear in a row.
                */
                while (( line = input.readLine()) != null){
                  contents.append(line);
                  contents.append(System.getProperty("line.separator"));
                }
              }
              finally {
                input.close();
              }
            }
            catch (IOException ex){
            }

            return contents.toString();
          }
        static public void setContents(File aFile, String aContents)
                                         throws FileNotFoundException, IOException {
            if (aFile == null) {
              throw new IllegalArgumentException("File should not be null.");
            }
            if (!aFile.exists()) {
              throw new FileNotFoundException ("File does not exist: " + aFile);
            }
            if (!aFile.isFile()) {
              throw new IllegalArgumentException("Should not be a directory: " + aFile);
            }
            if (!aFile.canWrite()) {
              throw new IllegalArgumentException("File cannot be written: " + aFile);
            }

            //use buffering
            Writer output = new BufferedWriter(new FileWriter(aFile));
            try {
              //FileWriter always assumes default encoding is OK!
              output.write( aContents );
            }
            finally {
              output.close();
            }
          }

          /** Simple test harness.
             * @param aArguments
             * @throws java.io.IOException */


        } 

`

2 个答案:

答案 0 :(得分:3)

您可以将角色转换为int。

例如:

String x = "asdf";
char[] y = x.toCharArray();

for (char z : y) {
    System.out.println((int) z); // cast char z to int - this can be done anywhere, but I'm doing it here to show the output.
}

输出:

97
115
100
102

答案 1 :(得分:-1)

我的帮助班。每当您想要将string转换为char时,请调用函数convertString(String s)

public class StringToAscii {

    public static int convertChar(char c) {
        return (int) c;
    }

    public static String convertString(String s) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            sb.append((int)s.charAt(i));
            sb.append(" ");
        }
        return sb.toString();
    }
}
相关问题