Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字符串转换为int,然后再转换回字符串_Java - Fatal编程技术网

Java 将字符串转换为int,然后再转换回字符串

Java 将字符串转换为int,然后再转换回字符串,java,Java,我使用类似于s=“DLP\u Classification\u Rules”的字符串来转换某个整数,并且再次使用该整数,我必须转换回类似于DLP\u Classification\u规则的相同字符串 是否有任何方法可以使用base64或其他方法执行此操作?如果要转换的字符串是常量,则可以为它们创建枚举: enum StringInteger{ DLP_分类规则(1), 一些其他字符串(2); 私有int值; StringInteger(int值){ 这个值=值; } 公共静态StringInte

我使用类似于s=“DLP\u Classification\u Rules”的字符串来转换某个整数,并且再次使用该整数,我必须转换回类似于DLP\u Classification\u规则的相同字符串


是否有任何方法可以使用base64或其他方法执行此操作?

如果要转换的字符串是常量,则可以为它们创建
枚举:

enum StringInteger{
DLP_分类规则(1),
一些其他字符串(2);
私有int值;
StringInteger(int值){
这个值=值;
}
公共静态StringInteger toInt(字符串值){
试一试{
返回StringInteger.valueOf(value);
}捕获(例外e){
抛出新的RuntimeException(“此字符串未与整数关联”);
}
}
公共静态StringInteger-toString(int值){
对于(StringInteger:StringInteger.values()){
if(stringInteger.value==value)返回stringInteger;
}
抛出新的RuntimeException(“此整数与字符串不关联”);
}
}

不使用Base64,因为结果不是整数,而是字符串。 您要寻找的基本上是将字符串无损压缩为数字(它将比int大得多),这并不容易

这完全取决于您的用例。您是否在单个JVM上运行单个应用程序?您正在运行客户端/服务器模块吗?在应用程序运行之间是否需要保留信息

例如,您可以使用映射将所有字符串映射到数字,并与周围的数字进行通信,并且无论何时需要字符串本身,都可以从映射中将其拉出。不过,如果您需要在本地JVM之外引用这些数字和字符串,那么除非您将映射转移到该JVM,否则不会有多大帮助。 您还可以将此映射持久化到文件或数据库,以便在应用程序之间共享信息。 映射中的整型键可以是一个简单的计数器,如“map.put(map.size(),s)”(但不要从映射中删除任何内容:),或者更复杂的方法是使用字符串的HashCode()函数,该函数返回一个int:“map.put(s.HashCode(),s)”

试试这个

static BigInteger strToInt(String s) {
    return new BigInteger(s.getBytes(StandardCharsets.UTF_8));
}

static String intToStr(BigInteger i) {
    return new String(i.toByteArray(), StandardCharsets.UTF_8);
}

输出:

1674664573062307484602880374649592966384086501927007577459
DLP_Classification_Rules
0
DLP_Classification_Rules
或者,您也可以像这样创建一个字符串池

public class StringPool {
    private final Map<String, Integer> pool = new HashMap<>();
    private final List<String> list = new ArrayList<>();

    public int stringToInteger(String s) {
        Integer result = pool.get(s);
        if (result == null) {
            result = pool.size();
            pool.put(s, result);
            list.add(s);
        }
        return result;
    }

    public String integerToString(int i) {
        if (i < 0 || i >= pool.size())
            throw new IllegalArgumentException();
        return list.get(i);
    }
}
输出:

1674664573062307484602880374649592966384086501927007577459
DLP_Classification_Rules
0
DLP_Classification_Rules

希望它能帮助您

package com.test;

import java.security.Key;

import javax.crypto.Cipher;

public class EncrypDES {

    private static String strDefaultKey = "des20200903@#$%^&";
    private Cipher encryptCipher = null;
    private Cipher decryptCipher = null;

    /**
     * @throws Exception
     */
    public EncrypDES() throws Exception {
        this(strDefaultKey);
    }

    /**
     * @param strKey
     * @throws Exception
     */
    public EncrypDES(String strKey) throws Exception {
        Key key = getKey(strKey.getBytes());
        encryptCipher = Cipher.getInstance("DES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);
        decryptCipher = Cipher.getInstance("DES");
        decryptCipher.init(Cipher.DECRYPT_MODE, key);
    }

    /**
     * @param arrBTmp
     * @return
     * @throws Exception
     */
    private Key getKey(byte[] arrBTmp) throws Exception {
        byte[] arrB = new byte[8];
        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }
        Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
        return key;
    }

    /**
     * @param strIn
     * @return
     * @throws Exception
     */
    public String encrypt(String strIn) throws Exception {
        byte[] encryptByte = encryptCipher.doFinal(strIn.getBytes());
        int iLen = encryptByte.length;
        StringBuffer sb = new StringBuffer(iLen * 2);
        for (int i = 0; i < iLen; i++) {
            int intTmp = encryptByte[i];
            while (intTmp < 0) {
                intTmp = intTmp + 256;
            }
            if (intTmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(intTmp, 16));
        }
        return sb.toString();
    }

    /**
     * @param strIn
     * @return
     * @throws Exception
     */
    public String decrypt(String strIn) throws Exception {
        byte[] arrB = strIn.getBytes();
        int iLen = arrB.length;
        byte[] arrOut = new byte[iLen / 2];
        for (int i = 0; i < iLen; i = i + 2) {
            String strTmp = new String(arrB, i, 2);
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
        }
        byte[] decryptByte = decryptCipher.doFinal(arrOut);
        return new String(decryptByte);
    }

    public static void main(String[] args) {
        try {
            String msg1 = "xincan001";
            String key = "20200903#@!";
            EncrypDES des1 = new EncrypDES(key);
            System.out.println("input value:" + msg1);
            System.out.println("After encryption Value:" + des1.encrypt(msg1));
            System.out.println("After decryption Value:" + des1.decrypt(des1.encrypt(msg1)));
            System.out.println("--------------");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


如果您乐于使用
biginger
并获得一些非常非常大的整数,您可以这样做。。。但我怀疑你不会对最终得到的整数的大小感到满意。如果您能为我们提供更多关于您试图做什么的背景,我们就更有可能帮助您。您能举一个例子说明DLP_分类规则
将转换为什么数字吗?这个值是不是一个不太长的静态值列表中的一个成员?它可以是任何整数值,但我不能给静态字符串,因为我会得到这个字符串是动态的。你会把一个整数转换成系统以前没有见过的字符串吗?或者,您是否只将整数转换回您的系统首先转换为该整数的字符串?也许我们可以看到您的代码?简单且达到目标!非常感谢。对于这里的解决方案,我可以看到String to BigInteger,但实际上我需要String to Integer如果您想要一个只使用大小写字母和下划线的24个字符的字符串,那么该信息量是
Math.pow(26+26+1,24)=2.4133531101151925e41
。它超过了
int(32位)
的精度。如果不转换回字符串,有没有办法得到唯一的整数。@madhusudhan更新了我的答案。谢谢。对于这里的解决方案,我可以看到字符串到加密值,但实际上我需要字符串到整数
input value:xincan001
After encryption Value:c2cc016fae0bd2008282cae3f2c0be62
After decryption Value:xincan001
--------------