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

930 Views Asked by At

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 = {'7','8','9'};
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't make a difference
    digits = new byte[inDigits.length]; //digits is a class (static final) variable
    for (int i = 0; i < inDigits.length; i++)
    {
        digits[i] = inDigits[i];
    }
    if(digits == null || digits.length == 0 || (digits[0] == digits.length && digits[0] == digits['-']))
    {
        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] + ", "); //printing from here
    }
    isNegative = isNeg; //should this be elsewhere?
    for (int i = 0; i < 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 '55'.
        {
            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) //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 < 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!

0

There are 0 best solutions below