Java:如何使byte[]数组返回硬编码数字,它目前返回的是什么?

huangapple 未分类评论44阅读模式
英文:

Java: How do you get a byte[] array to return hardcoded numbers and what is it that it's currently returning?

问题

以下是翻译后的代码部分:

byte[] b = {'7', '8', '9'};
MegaInt num4 = new MegaInt(b, false);

public MegaInt(byte[] inDigits, boolean isNeg) {
    inDigits.toString();
    digits = new byte[inDigits.length];
    for (int i = 0; i < inDigits.length; i++) {
        digits[i] = inDigits[i];
    }
    if (digits == null || digits.length == 0 || (digits[0] == digits.length && digits[0] == '-')) {
        throw new IllegalArgumentException(s);
    }
    int validIndex = 0;
    int leadingZeroCount = 0;
    boolean leadingZero = true;
    int stringLength = digits.length;
    for (int j = 0; j < digits.length; j++) {
        System.out.print(digits[j] + ", ");
    }
    isNegative = isNeg;
    for (int i = 0; i < stringLength; i++) {
        byte b = digits[i];
        System.out.println(b);
        if (!isValidDigit(b)) {
            throw new IllegalArgumentException(s);
        }
        if (leadingZero) {
            if (b != '0') {
                digits = new byte[stringLength - leadingZeroCount];
                digits[validIndex] = (byte) Character.getNumericValue(b);
                leadingZero = false;
            } else {
                leadingZeroCount++;
            }
        } else {
            validIndex++;
            digits[validIndex] = (byte) Character.getNumericValue(b);
        }
    }
    if (leadingZero) {
        digits = new byte[1];
        digits[0] = 0;
        isNegative = false;
    }
}

private boolean isValidDigit(byte b) {
    byte[] search = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    for (int j = 0; j < search.length; j++) {
        if (b == search[j]) {
            return true;
        }
    }
    return false;
}

此外,您提到代码返回了 "55,56,57,55",这实际上是字符编码的ASCII值,而不是您想要的数字。可能是因为在您的打印语句中,将字符直接与数字相加,导致ASCII值输出。如果要打印数字而不是字符的ASCII值,您可以使用以下方式进行修改:

将这行代码:

System.out.print(digits[j] + ", ");

修改为:

System.out.print((char) digits[j] + ", ");

将这行代码:

System.out.println(b);

修改为:

System.out.println((char) b);

这样做会将ASCII值转换为字符,并输出您所需的数字。

英文:

I have hard-coded a byte array (it has to be bytes and not integers for space reasons) and have called it to a constructor that takes a byte array:

this is my byte[] and the constructor from the test file.

byte[] b = {&#39;7&#39;,&#39;8&#39;,&#39;9&#39;};
MegaInt num4 = new MegaInt(b, false); //the second parameter is just a boolean to see if the number is negative or not

this is where it is called from the constructor in the class file:

public MegaInt(byte[] inDigits, boolean isNeg)
{
    inDigits.toString(); //tried turning to string here but doesn&#39;t make a difference
    digits = new byte[inDigits.length]; //digits is a class (static final) variable
    for (int i = 0; i &lt; inDigits.length; i++)
    {
        digits[i] = inDigits[i];
    }
    if(digits == null || digits.length == 0 || (digits[0] == digits.length &amp;&amp; digits[0] == digits[&#39;-&#39;]))
    {
        throw new IllegalArgumentException(s);
    }
    int validIndex = 0; 
    int leadingZeroCount = 0;
    boolean leadingZero = true;
    int stringLength = digits.length;
    for (int j = 0; j &lt; digits.length; j++)
	{
		System.out.print(digits[j] + &quot;, &quot;); //printing from here
	}
    isNegative = isNeg; //should this be elsewhere?
    for (int i = 0; i &lt; stringLength; i++)
    {
        byte b = digits[i]; //does not work for charAt
        System.out.println(b);
        if(!isValidDigit(b)) //gets here and throws exception because of the &#39;55&#39;.
        {
            throw new IllegalArgumentException(s);
        }
        if (leadingZero)
        {
            if (b != &#39;0&#39;)
            {
                digits = new byte[stringLength - leadingZeroCount];
                digits[validIndex] = (byte)Character.getNumericValue(b);
                leadingZero = false;
            }
            else
            {
                leadingZeroCount++;
            }
        }
        else
        {
            validIndex++;
            digits[validIndex] = (byte)Character.getNumericValue(b);
        }
    }
    if (leadingZero) //if there is only 1 zero in the array.
    {
        digits = new byte[1];
        digits[0] = 0; 
        isNegative = false;
    }

private boolean isValidDigit(byte b) //checks if digits are proper for Byte constructors. 
{
    byte[] search = {1,2,3,4,5,6,7,8,9,0};
    for (int j = 0; j &lt; search.length; j++)
    {
        if (b == search[j])
        {
            return true; //for valid digits. 
        }
    }
    return false;
}

this is what it is returning:

55,56,57,55

I figure this must be some sort of byte representation (I don't think it's binary or hex?) but obviously this is not what I want, how do I get it to use the numbers I have hard-coded? OR am I initializing the byte array properly? Why is it returning 4 numbers when the array only has three? I feel like this is a very simple problem and yet I am finding nothing relevant online so I hope there is someone who can help!

huangapple
  • 本文由 发表于 2020年4月11日 01:12:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61145223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定