java 从 char 转换为 long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29997665/
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-11-02 16:16:07  来源:igfitidea点击:

Converting from a char to a long

javaarrayslong-integerbiginteger

提问by magna_nz

I'm creating a big integer class. I read in two longs as function arguments and what I want to do is have each number in the long read in to occupy one index of an array.

我正在创建一个大整数类。我读入两个 long 作为函数参数,我想要做的是让 long 读入中的每个数字占据数组的一个索引。

I've done this by converting the long into a string then char array and storing them somewhere temporarily. This works fine and when printed out prints out exactly like the number read in. Now what I want to do is add them now to a new array and have their data type as long. I have made two arrays to deal with this.

我通过将 long 转换为字符串然后转换为字符数组并将它们临时存储在某个地方来完成此操作。这很好用,当打印出来时,打印出来的数字与读入的数字完全一样。现在我想要做的是将它们现在添加到一个新数组中,并将它们的数据类型设为long. 我制作了两个数组来处理这个问题。

Problem is when I try cast the char to a long, it gives me a completely different value. It seems that it's converting the char into it's own respective number? Character class doesn't seem to have any way of converting to a long.

问题是当我尝试将字符转换为 long 时,它给了我一个完全不同的值。似乎它正在将字符转换为它自己的相应数字?字符类似乎没有任何方法可以转换为 long。

What's the best way to do this?

做到这一点的最佳方法是什么?

EDIT: It seems that if I change the long arrays to int arrays and then use Character.getNumericValue(char ch) it works adding it to the array properly.

编辑:似乎如果我将长数组更改为 int 数组,然后使用 Character.getNumericValue(char ch) 它可以正确地将它添加到数组中。

Since I'm planning to be returning a long at the end of this function should I make sure that those arrays are long for safety? Or storing them as ints in the array is fine? Thanks

因为我打算在这个函数的末尾返回一个 long ,我应该确保这些数组是 long 以确保安全吗?或者将它们作为整数存储在数组中可以吗?谢谢

public static long hugeMultiplication(long value1, long value2){
    System.out.println("originalvalue: "+value1);
    int lengthOfWordOne = String.valueOf(value1).length();
    int lengthOfWordTwo = String.valueOf(value1).length();
    System.out.println("length1: "+lengthOfWordOne);
    System.out.println("length2: "+lengthOfWordTwo);

    long[] numberOne = new long[lengthOfWordOne+1];
    long[] numberTwo = new long[lengthOfWordTwo+1];

    //make those longs into string to convert char array
    char[] tempValueOne = String.valueOf(value1).toCharArray();
    char[] tempValueTwo = String.valueOf(value2).toCharArray();

    //copy each value of a char array to long array and change to long again
    //this will set up the array having each number in it so we can do the multiplication
    for (int i = 0; i < tempValueOne.length; i++){
        numberOne[i] = (long) tempValueOne[i];
    }


    for (int i = 0; i < numberOne.length; i++){
        System.out.print(numberOne[i]);
    }

回答by Jared

The only real difference between a charand a longis the data size. A charis typically 1 byte, while a longis typically 8 bytes. When you convert a longto a string, you are creating an array of ASCII characters that would represent this number. For example, the long12345 would be represented in memory as:

achar和 a之间唯一真正的区别long是数据大小。Achar通常为 1 个字节,而 along通常为 8 个字节。当您将 a 转换long为 a 时string,您正在创建一个代表该数字的 ASCII 字符数组。例如,long12345 在内存中将表示为:

00000000 00000000 00000000 00000000 00000000 00000000 00110000 00111001

But when converted to a string(an array of 1-byte chars), it would look more like this:

但是当转换为 a string(一个 1 字节char的数组)时,它看起来更像这样:

00110001 00110010 00110011 00110100 00110101

Where each byte is a character in the string. The first byte being an ASCII '1', '2', etc.

其中每个字节都是字符串中的一个字符。第一个字节是 ASCII '1'、'2' 等。

When you cast each charback to a long, you are actually getting the ASCII value of each character in the string. So the char'1' is actually represented in memory as an integer 49, '2' is '50', and so on. What you will need to do is convert the string as a whole back to the integer representation.

当您将每个转换char回 a 时long,您实际上获得了字符串中每个字符的 ASCII 值。所以char'1'在内存中实际上表示为一个整数49,'2'是'50',依此类推。您需要做的是将整个字符串转换回整数表示形式。