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

rdlzhqv9  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(381)

我有像s=“dlp\u classification\u rules”这样的字符串来转换一些整数,然后用这个整数,我必须转换回像dlp\u classification\u rules一样的字符串。
有没有什么方法可以用base64或者别的什么来做到这一点。?

ijnw1ujt

ijnw1ujt1#

如果要转换的字符串是常量,则可以创建 Enum 对他们来说:

enum StringInteger {
    DLP_Classification_Rules(1),
    Some_Other_String(2);

    private int value;

    StringInteger(int value) {
        this.value = value;
    }

    public static StringInteger toInt(String value) {
        try {
            return StringInteger.valueOf(value);
        } catch (Exception e) {
            throw new RuntimeException("This string is not associated with an integer");
        }
    }

    public static StringInteger toString(int value) {
        for (StringInteger stringInteger : StringInteger.values()) {
            if (stringInteger.value == value) return stringInteger;
        }
        throw new RuntimeException("This integer is not associated with a string");
    }
}
egdjgwm8

egdjgwm82#

希望能对你有所帮助

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

    }
}

运行结果

input value:xincan001
After encryption Value:c2cc016fae0bd2008282cae3f2c0be62
After decryption Value:xincan001
--------------
k2fxgqgv

k2fxgqgv3#

试试这个。

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

String s="DLP_Classification_Rules";
BigInteger i = strToInt(s);
System.out.println(i);
String r = intToStr(i);
System.out.println(r);

输出:

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

StringPool pool = new StringPool();
String s = "DLP_Classification_Rules";
int i = pool.stringToInteger(s);
System.out.println(i);
String r = pool.integerToString(i);
System.out.println(r);

输出:

0
DLP_Classification_Rules
kgsdhlau

kgsdhlau4#

base64不是,因为结果不是整数,而是字符串。你要找的基本上是把字符串无损压缩成一个数字(比int大得多),这并不容易。
这完全取决于您的用例。您是否在单个jvm上运行单个应用程序?您正在运行客户机/服务器模块吗?在应用程序运行期间是否需要保留信息?
例如,您可以使用map<integer,string>将所有字符串Map为数字,并将这些数字传递给其他人,每当您需要字符串本身时,就从Map中提取它。不过,如果您需要在本地jvm之外引用这些数字和字符串,那么除非您将Map转移到该jvm,否则不会有多大帮助。您还可以将此Map持久化到文件或数据库,以便在应用程序之间共享信息。Map中的整数键可以是一个简单的计数器,如“map.put(map.size(),s)”(但是不要从Map中删除任何内容:);或者更复杂的方法是使用字符串的hashcode()函数,该函数返回int:“map.put(s.hashcode(),s)”

相关问题