尝试将 char 数组的元素转换为 int 时出错

Getting an error when trying to convert an element of a char array to int

以下是我目前使用的代码:

public static String encrypt(String password) {
    String encrypted = null;
    char passChars[] = password.toCharArray();
    int ascii[] = null;
    for(int i=0;i<passChars.length;i++) {
        ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));
        ascii[i] = ascii[i] + 17;
        passChars[i] = (char)ascii[i];
        encrypted = encrypted + String.valueOf(passChars[i]);
    }
    return encrypted;
}

当我尝试 运行 时,它得到 运行 但是当我使用加密方法时,我得到以下错误:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "M"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.package.program.Encryptor.encrypt(Encrypter.java:46)
at com.package.program.Main.actionPerformed(Main.java:136)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我试过改变一些东西,例如改变将 char 转换为 int 的方式,但似乎没有任何效果...我做错了什么?

如果你想 "encrypt" 通过将每个字符偏移一定量来 "encrypt" 字符串,那么直接这样做:

String encrypted = null;
char passChars[] = password.toCharArray();
for (int i=0; i < passChars.length; i++) {
    passChars[i] += 17;
}
encrypted = new String(passChars);

Demo

请注意,向某些 ASCII 字符添加 17 可能会使您遇到无法打印的内容。目前尚不清楚您要在这里实现的目标;如果你想系统地将密码加扰为其他字符,那么我们可能不得不使用模数来绕过。

这一行是错误的:

ascii[i] = Integer.parseInt(String.valueOf(passChars[i]));

您正在尝试将密码的字母转换为整数。不能保证密码只由数字组成。

相反,您可以这样做:

ascii[i] = passChars[i] + 17;

直接。

使用有效大小初始化 ascii[],否则您可能会遇到索引超出范围的异常,并且此程序仅适用于作为数字字符串的密码。