# parseInt() problem

**URL:** https://forum.kirupa.com/t/parseint-problem/13436
**Category:** flash
**Created:** [February 13, 2003, 6:01am UTC](https://forum.kirupa.com/t/parseint-problem/13436 "2003-02-13T06:01:35Z")
**Posts on this page:** 9
**Page:** 2

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 11:16am UTC](https://forum.kirupa.com/t/parseint-problem/13436/21 "2003-02-13T11:16:35Z")

</div>

:sure: :beam:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 11:31am UTC](https://forum.kirupa.com/t/parseint-problem/13436/22 "2003-02-13T11:31:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 11:39am UTC](https://forum.kirupa.com/t/parseint-problem/13436/23 "2003-02-13T11:39:09Z")

</div>

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++; }

😛

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 11:58am UTC](https://forum.kirupa.com/t/parseint-problem/13436/24 "2003-02-13T11:58:42Z")

</div>

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

```auto

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));

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 12:02pm UTC](https://forum.kirupa.com/t/parseint-problem/13436/25 "2003-02-13T12:02:50Z")

</div>

…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 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 12:24pm UTC](https://forum.kirupa.com/t/parseint-problem/13436/26 "2003-02-13T12:24:45Z")

</div>

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

thanx senocular!=)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 12:48pm UTC](https://forum.kirupa.com/t/parseint-problem/13436/27 "2003-02-13T12:48:20Z")

</div>

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));**

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 13, 2003, 4:40pm UTC](https://forum.kirupa.com/t/parseint-problem/13436/28 "2003-02-13T16:40:02Z")

</div>

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:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [February 14, 2003, 4:41am UTC](https://forum.kirupa.com/t/parseint-problem/13436/29 "2003-02-14T04:41:29Z")

</div>

:A+:

[Previous page](https://forum.kirupa.com/t/parseint-problem/13436.md?page=1)
