Domanda

I was curious how to muliply an array by a factor? Not each cell (t[0], t[1], etc) individually, but as a whole number. Ex: t[0] = 9 t[1] =2 t[2] = 5, t[] = 925. 925 times 3 =2775

Basically, I am receiving a value and converting from ASCII to Decimal(I have already done this). However, I want to multiply by it a factor of 3. Do I need to store the entire array as a string, and then use the multiply function?

The relevant code for this section

byte[] readBuf =(byte[]) msg.obj);
char x;
String readMessage = newString(readBuf,0,msg.arg1);
int[] t = new int[readMessage.length()];
for(int i = 0; i<readMessage.length(); i++)
{
    x = readMessage.charAt(i);
    int z = (int) x;//Array has been converted from ASCII into decimal values
    t[i] = z;//Array has been populated with decimal values

    //Confused about the next part, Convert back into string and then multiply string?
}
È stato utile?

Soluzione

Why make extra variables for char and int, use space for tables and pass things from one variable to another when you can do it all in one line? From what i've understood this is all you need.

byte[] readBuf =(byte[]) msg.obj);
String readMessage = newString(readBuf,0,msg.arg1); //You create the string here
String final=""; //new string to be parsed
for(int i = 0; i<readMessage.length(); i++){ 
    final+=""+(int)readMessage.charAt(i); // get the charAt(i) cast it to int and give it to the string
}
return Integer.parseInt(final)*factor; //return the int multiplied by 3

Altri suggerimenti

Edit: Use Double.parseDouble(readMessage) to convert it to decimal

ASCII characters are only Integer numbers

I'm not sure if I understand the format of your input string. Does this example solve your problems?

public static void main(String args[]) throws Exception {
    String input = "925";

    int parsed = Integer.parseInt(input);
    parsed *= 3;

    System.out.println(parsed); // prints 2775
}

Your ask is not clear, I try to understand: you have a String in an array where the zero index is the most significative position and the last is the less significative position. So, if you have 123, the situation is t[0]=1, t[1]=2, t[2]=3. If it's correct, then you say you want to rebuild the number (in my example 123 and return that multiplyed by a factor, i.e. 3). So return 369. Here's my solution assuming that the resulting number will be lower than MAXINT. I also mantain your code as is, and return the result multiplyed by a factor in value "factor" (int).

byte[] readBuf =(byte[]) msg.obj);
char x;
String readMessage = newString(readBuf,0,msg.arg1);
int[] t = new int[readMessage.length()];
String final="";
for(int i = 0; i<readMessage.length(); i++)
{
    x = readMessage.charAt(i);
    int z = (int) x;//Array has been converted from ASCII into decimal values
    t[i] = z;//Array has been populated with decimal values
    final+=""+t[i]; // add to String the char in position i.
    //Confused about the next part, Convert back into string and then multiply string?
}
return Integer.parseInt(final)*factor;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top