parseInt() problem

:sure: :beam:

wow, i didn’t expect so many replies, but all the replies are not related to this thread:bad: just joking, i’m glad this thread help to improve relation between lost:love: cyanblue. :beam:

ok, i reexplain my question, i want to do a simple program that will do radix conversion.

user input the original base and number. then input the new base, and the program return the answer.
the program can check that whether the number input by user is valid or not.

the conversion between number can easily done by toString and parseInt, but these two do not do the validation of the number.

i remember that last time i do validation check in java using substring. kinda like this :

int Count=0;
while(Count<mynumber.length()){
Checkint = 0;
Checkint = new Integer(mynumber.substring(Count,Count+1)).intValue();
if (Checkint>=Oldradix) Flag = false;
Count++; }

i don’t sure the coding is correct or not, because it was long time ago when do this program.

i just haven’nt figure out how to do this in AS.:sigh:

don’nt whether i can translate it to AS or not.

sorry little mistake with that coding. the while should look like this: it weird, while i type this line no spacing the
< mynumber.length() do not appear,

int Count=0;
while(Count < mynumber.length() ){
Checkint = 0;
Checkint = new Integer(mynumber.substring(Count,Count+1)).intValue();
if (Checkint>=Oldr) Flag = false;
Count++; }

:stuck_out_tongue:

yeah thats my initial thought on validation is just looping through and checking the number to be within the radix. Youd have to go through and do that for letters as well.

something like


convert = function(num, rdx){
	if (2 > rdx || rdx > 36) return "Error: invalid radix (" + rdx + ") passed";
	if (typeof num != "string") num = num.toString();
	var dig, i = num.length;
	while (i--){
		var char = num.charAt(i);
		if (isNaN(char)) dig = char.toLowerCase().charCodeAt(0) - 87;
		else dig = number(char);
		if (dig >= rdx) return "Error: invalid digit \"" + char + "\"";
	}
	return parseInt(num, rdx);
}
trace(convert("1af",16));
trace(convert("1ag",16));

…also I kind of came into this thread a little late and I havent really gone through the previous stuff so I apologise if this is something that was already covered heh :slight_smile:

wow, thats code look a bit differennce from my java code, but never mind, i 'll try to figure it out.

thanx senocular!=)

I can go through it:

// define function with number and radix arguments
convert = function(num, rdx){

// check to see that the radix is within the appropriate
// values of 2 and 36
if (2 > rdx || rdx > 36)
// if so, return an error string
return “Error: invalid radix (” + rdx + “) passed”;

// check to see if the number passed is a string
if (typeof num != “string”)
// if not make it one. It should be since we might be dealing
// with characters and not just numbers
num = num.toString();

// sets some local variables for the function
// dig: represents the current digit as we cycle through the
// characters in num - digit isnt actually a ‘correct’ term because
// its more of a number value but I dont use digit enough and its
// a cool word I think
// i: is the the length of num which we use to cycle through in
// the while loop, decrementing it to 0 to kill the loop.
var dig, i = num.length;

// while i is real
while (i–){

// define char as the current ‘letter’ (string) at this position in num
var char = num.charAt(i);

// check to see if char is a number or letter
if (isNaN(char))
// if a letter (is Not a Number) then force it to lowercase if its not
// already and get its charcode (since its only one letter, 0 is its
// position in the string) and subtract 87 so your number is based
// at a = 10 where a’s normal charcode is 97 (we need 10 for
// the proper conversion over to numberland)
dig = char.toLowerCase().charCodeAt(0) - 87;
// if a number, assign dig to be the number of that char
else dig = number(char);

// if the digit is not within the range of the radix
if (dig >= rdx)
// return the error string
return “Error: invalid digit “” + char + “””;

// end while loop
}

// assuming the function wasnt terminated by the other returns
// which validate, parseInt can then be used and returned
return parseInt(num, rdx);

// end function
}

// test function
trace(convert(“1af”,16));
trace(convert(“1ag”,16));

thanx sen for explaining to me, you help me save some time to figure it out.:beam:

and you are really good at coding! :nerd: :nerd:

:A+: