238Views15Replies

Author Options:

If I want to convert the number 2, 5, 7 into 257, and also do the inverse (257 to 2, 5, 7)? how do I do that in code? Answered

If I want to convert the number 2, 5, 7 into 257, and also do the inverse (257 to 2, 5, 7)? how do I do that in code?

I have considered using math to do this: 2, 5, 7;   2*100 + 5*10 + 7 = 257; but This will only work with numbers with X number of digits. I need to to work with a variable amount of numbers in an array or stored in separate variables.

Also, I need to do the reverse, so how would I go about doing that? particularly in C, arduino, and Javascript. 

If anyone wants to know why I need to do this, I am trying to program a simple calculator app in java, and I would find this useful with the arduino too. I am learning programming here, and this is currently the calculator app: https://www.khanacademy.org/cs/calculator/5724205359824896. It is not finished, but I got over 200 lines of code to draw out the buttons and screen, and make them interactive. I need a way to capture the presses of the buttons and store them in arrays that when an operator is pressed,  a new array is created with more numbers inside it and those numbers are joined together. 

19 Replies

user
seandogue (author)2014-07-21

In javascript the solution for the first part (creating a string representation of the number) is easy

var blah1 = 2;

var blah2 = 5;

var blah3 = 7;

var numer = blah1.toString() + blah2.toString() + blah3.toString();

numer is now a string representation of the number 257;

In javascript, to split the number 257 into three (3) pieces, one can do this

var a = 257;

var b = a.toString();

var c = b.split("");

c is an array => ["2","5","7"]

to use those numbers you simply call the element and revert it to a number like so

var d = toNumber(c[i]);

or

var d = parseInt(c[i]);

where i (of c[i]) is the element you want to see, ie, 0, 1, or 2

You can iterate over the array easily, using a FOR loop and using the array length (c.length();) to limit its action.

Select as Best AnswerUndo Best Answer

user
-max- (author)seandogue2014-07-24

Great, just one problem: I have between 0-8 values in an array, and I need to do this operation with them. I thought I could do this, but it returns an error.

var i = 0;

while (i < array1.length){

array1value = array1[array1.length].toString();

i += 1;

}

Select as Best AnswerUndo Best Answer

user
-max- (author)-max-2014-07-24

I had high hopes for this: how do I allow it to continuously add however many things are in an array? I can't end a line w/o a ";"

for (var i = 0; i < array1.length; i += 1){

array1value = array1[i].toString() + ;

}

array1value = array1[array1.length-1].toString()

Select as Best AnswerUndo Best Answer

user
seandogue (author)-max-2014-07-25

I'm apologize, but this is going well beyond the initial question. I'd recommend that you find an online course in programming if you can't do this for yourself.

I noted W3 schools earlier. Google W3Schools and go there, or find another online site that helps people learn to program.

Best wishes

Select as Best AnswerUndo Best Answer

user
-max- (author)seandogue2014-07-25

I didn't think it was, since I am still doing the same thing. I just cant seem to get the for loop to work, Surely I am missing someting? I have got the string to work, but only with the following code. I want to shorten it and I have tried for and while loops, but they for one reason or another do not work.

if (array1.length < 8){

if (one === true && P_one === false){

array1.push(1);

}

if (two === true && P_two === false){

array1.push(2);

}

if (three === true && P_three === false){

array1.push(3);

}

if (four === true && P_four === false){

array1.push(4);

}

if (five === true && P_five === false){

array1.push(5);

}

if (six === true && P_six === false){

array1.push(6);

}

if (seven === true && P_seven === false){

array1.push(7);

}

if (eight === true && P_eight === false){

array1.push(8);

}

if (nine === true && P_nine === false){

array1.push(9);

}

if (zero === true && P_zero === false){

array1.push(0);

}

if (dec === true && P_dec === false){

array1.push(".");

}

}

Select as Best AnswerUndo Best Answer

user
-max- (author)seandogue2014-07-24

Great! I did get that working! But the number of 'blahs' depends on how many presses of the keypad. Then I guess that will be stored in an array after pressing an operator, and a new array is created for more keypad presses.

Select as Best AnswerUndo Best Answer

user
seandogue (author)-max-2014-07-24

yep, that's where the "c.length()" comes into play

If you take a string and split it using "", it will slice it up into one character-wise entries (I believe...haven't done it in a little while)

if you then use the javascript length() method on the resultant array, you can determine the number of entries you have to deal with.

If you then use that value to iterate you can call back each entry without scratching your head to figure out "How many of those are there?)

W3 schools has quite a bit of human readable info on javascript (as do other sites forums, IRC, etc.)

So what I'd do is this

1) user enters number

2) you store number to variable

3) you create a new variable to hold the string representation of the value

4) you split the string (split only works on strings) as noted (you can also split a string that has commas, or dashes, white spce (spaces), or pretty much any character, by specifying the value about which you want to split a string into an array)

ex:

var user_entry = "the dogs go to play in the rain";

var split_words = user_entry.split(" ");

(split_words is an array ["the", "dogs", "go", "to", "play", "in", "the", "rain"]

and var length_of_split_words = split_words.length();

is equal to 8

Now, using a new version of the for loop I noted earlier in this thread

for (var i=0; i < length_of_split_words; i++){

Do something or another with split_words[i];

}

No effort beyond the initial coding is required to determine the number of entries in the array

Select as Best AnswerUndo Best Answer

user
seandogue (author)seandogue2014-07-24

(I hate that I still can't edit a reply after nearly 7 years on this site. jeez ible-folk, get you stuff together)

Anyway... I forgot to include the neato thing.

inside that for loop,presuming you were working with numbers to start, converting them into single character strings via the String() and Split() methods, you can "unconvert" each to a numeric value by using either toNumber or parseInt()

split_words[i].toNumber;

or

parseInt(split_words[i]);

Select as Best AnswerUndo Best Answer

user
-max- (author)seandogue2014-07-24

As of so far it turns out I do not need to know how to convert the string back into individual characters, though I will use that knowledge with my arduino (hence why I also asked for 'C'!) I have this code working, but I want to get redundancy down by using something like a for loop. I am sure it is easy or there is something I am missing. The code is on line 396-483 here: https://www.khanacademy.org/cs/calculator/5724205359824896

Select as Best AnswerUndo Best Answer

user
kelseymh (author)2014-07-21

1) To convert a terminating sequence of digits to a decimal value, you can use sequential multiplication by ten. Let the digits be d1, d2, d3 ... dn, where 'n' may not be known, but a terminator (e.g., NUL or -1) is available. Then

value = (((d1*10) + d2)*10 + d3) * 10 ... ;

Alternatively, you can write at each cycle, "value *= 10 + d;", where value must be initialized to zero, and 'd' is the digit for that cycle.

2) To convert a decimal value to a sequence of digits, you can use modulo arithmetic and remainders. You'll need to reverse the order of the list of digits aquired this way, or use an (expensive) log10() call up front to get the number of digits.

while (value > 0) {

digit = value%10; // This gives the LSB (ones-place) digit

value /= 10; // This shifts the number down by one place

}

Obviously, you'll need to collect the "digit" value into an array or vector or something.

Select as Best AnswerUndo Best Answer

user
-max- (author)kelseymh2014-07-24

N is not only unknown, but can be (an integer) between 0-8. It is variable. Also, I need decimal support too.

Select as Best AnswerUndo Best Answer

user
kelseymh (author)-max-2014-07-25

You realize that what you're doing is trying to write a general purpose text parser, as a special case. You might want to read up on Lexx and Yacc.

What you could be doing, I think, is processing the keystrokes one by one: instead of collecting a variable-length array, just build up the numeric value directly as you receive characters, using the algorithm I outlined above.

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)kelseymh2014-07-22

i discovered using the string handling to strip a number down works really well too. I doubt it's faster, but it's easy to read.....

Select as Best AnswerUndo Best Answer

user
iceng (author)2014-07-21

Treat 2,5,7 as text and contentate the text to a string of 257 and then call a text to numeric conversion routine.

Select as Best AnswerUndo Best Answer

user
-max- (author)iceng2014-07-21

Can you show me some pseudo code? I don't understand how to do all that. Thank you.

Select as Best AnswerUndo Best Answer

user
iceng (author)-max-2014-07-21

I do Compiled-Real-Basic, Fortran, over 30 machine languages some 'C' code BUT No Java and don't know pseudo code.

The math is easier to show,

Find the second digit of 257 .....

Divide by 10 => 257 / 10 = 25.7 and convert to integer => 25

Divide that by 10 => 25 / 10 = 2.5 and convert to integer => 2

Multiply that by 10 => 22 x 10 = 20

Subtract | 25 - 20 | = 5

Answer => Second digit is 5

Do you need a flow-chart ?

Select as Best AnswerUndo Best Answer

user
-max- (author)iceng2014-07-24

Seems quite convoluted. Also the number of digits in the series is variable, at least up to 8. What about decimals? I guess it would be much the same?

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)-max-2014-07-21

string=integerToString (number)

for ctr=1 to length(string)

Print string(ctr)

Select as Best AnswerUndo Best Answer

user
iceng (author)iceng2014-07-21

To work in reverse there is a simple compiled real basic routine to pull any single digit value from the right front of the least significant number.

Another way is to divide a copy by ten, convert to integer, multiply by ten, and the difference is the units digit repeat for the others :-)

Select as Best AnswerUndo Best Answer