Java - 将字母字符串转换为相应 ascii 的 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13405855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:36:28  来源:igfitidea点击:

Java - Convert a String of letters to an int of corresponding ascii?

javaascii

提问by AaronAAA

I want to convert a String, lets say "abc", to an int with the corresponding ascii: in this example, 979899.

我想将 String 转换为"abc"具有相应 ascii 的 int:在本例中,979899.

I've run into two problems:

我遇到了两个问题:

1) what I wrote only works for characters whose ascii is two characters long and

1) 我写的只适用于 ascii 为两个字符长的字符,并且

2) since these numbers get very big, I can't use longs and I'm having trouble utilizing BigIntegers.

2) 由于这些数字变得非常大,我不能使用 long,而且我在使用 BigIntegers 时遇到了麻烦。

This is what I have so far:

这是我到目前为止:

BigInteger mInt = BigInteger.valueOf(0L);
for (int i = 0; i<mString.length(); i++) {
        mInt = mInt.add(BigInteger.valueOf(
                (long)(mString.charAt(i)*Math.pow(100,(mString.length()-1-i)))));
}

Any suggestions would be great, thanks!

任何建议都会很棒,谢谢!

采纳答案by arshajii

What's wrong with doing all the concatenation first with a StringBuilderand then creating a BigIntegerout of the result? This seems to be much simpler than what you're currently doing.

首先使用 a 进行所有连接StringBuilder,然后BigInteger从结果中创建 a有什么问题?这似乎比您目前正在做的要简单得多。

String str = "abc";  // or anything else

StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
    sb.append((int)c);

BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);

回答by Kent

you don't have to play the number game. (pow 100 etc). just get the number string, and pass to constructor.

你不必玩数字游戏。(战俘 100 等)。只需获取数字字符串,然后传递给构造函数。

final String s = "abc";
        String v = "";
        final char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            v += String.valueOf((int) chars[i]);
        }
//v = "979899" now
        BigInteger bigInt = new BigInteger(v); //BigInteger
        BigDecimal bigDec = new BigDecimal(v); // or BigDecimal

回答by mdgeorge

To handle n-digit numbers, you will have to multiply by a different power of ten each time. You could do this with a loop:

要处理 n 位数字,您必须每次乘以不同的 10 次幂。你可以用一个循环来做到这一点:

BigInteger appendDigits(BigInteger total, int n) {
    for (int i = n; i > 0; i /= 10)
        total = total.multiply(10);

    return total.plus(new BigInteger(n));
}

However, this problem really seems to be about manipulating strings. What I would probably do is simply accumulate the digits int a string, and create a BI from the String at the end:

然而,这个问题似乎真的是关于操作字符串。我可能会做的只是简单地将数字累积为一个字符串,并在最后从字符串创建一个 BI:

StringBuilder result = new StringBuilder();
for (char c : mString.getBytes())
    result.append(String.valueOf(c));
return new BigInteger(result.toString());