java equilant for charAtCode

Hi..in JS there is a function called charAtCode...i am wondering if there is a way to get a java equilant for that? i didnt see it in the String class itself.
[172 byte] By [bhaarat_javaa] at [2007-11-27 3:08:44]
# 1
What does it do? When I googled it, I only got a few results and they were all in French or Dutch.
DrClapa at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 2
What does charAtCode do?I'm guessing it translates between French and Dutch!Message was edited by: DrLaszloJamf
DrLaszloJamfa at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 3

i had that problem as well. all i got was bunch of chinese results lol. newhose..i found out that it basically just translates to a decimal equilant. so in java i did

char ch = 'd';

System.out.pritnln("charAtCode = " + (int) ch);

however, now i've run into a bigger problem :(. in JS there is a function

parseInt (string, radix);

..so if i have parseInt ("blah blah balh", 16); it will return result in base 16.

http://www.jibbering.com/faq/faq_notes/type_convert.html#tcPrIntRx

how the heck would i do THAT in java!!

bhaarat_javaa at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 4
[url] http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#valueOf(java.lang.String,%20int)[/url]
CaptainMorgan08a at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 5

Let's get really adventurous and try

[url=http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)]http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int)[/url]

As for the original question, I think you're talking about the charCodeAt method, which is equivalent to Java's charAt method. JavaScript also has a charAt method, but it returns a one-character-long string, since JS doesn't have a char data type.

uncle_alicea at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 6

thanks a lot guys!!

idk if its the end of the day thing but i am pulling my hair out trying to convert this **** JS code to java. maybe my first mistake is to that i am trying to convert it line by line!! so alot of place i have to cast stuff back. since JS doesnt care about casting...u just declare something with var and put nething u like in there. (messy).

ne suggestions?

function decrypt(str, pwd) {

var prand = "";

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

prand += pwd.charCodeAt(i).toString();

}

var sPos = Math.floor(prand.length / 5);

var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));

var incr = Math.round(pwd.length / 2);

var modu = Math.pow(2, 31) - 1;

var salt = parseInt(str.substring(str.length - 8, str.length), 16);

str = str.substring(0, str.length - 8);

prand += salt;

while(prand.length > 10) {

prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();

}

prand = (mult * prand + incr) % modu;

var enc_chr = "";

var enc_str = "";

for(var i=0; i<str.length; i+=2) {

enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));

enc_str += String.fromCharCode(enc_chr);

prand = (mult * prand + incr) % modu;

}

return enc_str;

}

besides getting coffee lol>

bhaarat_javaa at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 7

> ne suggestions?

My first suggestion is that you use standard English: "any" instead of "ne", "I don't know" instead of "idk", etc.. Communication can be difficult enough around here; we should all at least try to speak the same language.

Looks like the main challenge is keeping track of when things should be treated as strings and when they're supposed to be ints or chars. You can't call methods like toString() on a Java primitives, so you'll need to do something else, like wrapping the expression in a call to String.valueOf(). Even in those cases where the toString() is implicit, you may need to help it along. For instance, this line var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2)

+ prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));

could be translated to int mult = Integer.parseInt("" + prand.charAt(sPos) + prand.charAt(sPos*2)

+ prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));

Adding the empty string at the beginning forces the expression to be treated as a string instead of an integer. But in the case of var prand = "";

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

prand += pwd.charCodeAt(i).toString();

}

you want to do just the opposite. Assuming I'm reading it correctly, this loop creates a new string in which each character of the original is replaced with a string representation of the numeric encoding of the character. That means you have to force Java not to treat the individual characters as chars. The most direct translation would be String prand = "";

for (int i = 0; i < pwd.length(); i++) {

prand += (int)(pwd.charAt(i));

}

Later, you'll need to do a similar coercion as you translate enc_str += String.fromCharCode(enc_chr);

to enc_str += (char)enc_chr;

I assume you've already shot the guy who wrote the JS, and that's why you aren't forcing him to do this translation. ^_^

uncle_alicea at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 8

hahaha YES the JS guy should get served!

Finally I am done changing the code and I have to say..it was a good experience. on Some segments of code..JS looks simple..but still coming from Java Background it looks messy to me.

now I have to go back and change some of those constants..since i posted the code here :)

bhaarat_javaa at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...
# 9
> now I have to go back and change some of those> constants..since i posted the code here :)Yeah, I wondered about that...
uncle_alicea at 2007-7-12 3:57:05 > top of Java-index,Java Essentials,Java Programming...