Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Equvivalent in java the "String.fromCharCode"

Status
Not open for further replies.

jumper2

Programmer
Jul 13, 2007
41
ES
Hello,

I have a javascript which is able to crypt an e-mail address on my .jsp side like :
crypted += String.fromCharCode (r.charCodeAt (i) + 1);

I would like to take out this javascript from my .jsp and implement in .java the same functionality.
So far I have done already this like:
crypttext += (myaddr.charAt(i))+1;

My question is, how can I have the same result in .java as I have in the .javascript?

Thank you in advance!
 
I don't know JavaScript that well, so it might be helpful if you were a bit more specific about the behaviour you expect from your JavaScript version ...

Tim
 
I think you approach should work. What errors are you getting?

Cheers,
Dian
 
yes, the javascript itself works. There is no problem with.
But what I want that I execute this part:
" crypted += String.fromCharCode (r.charCodeAt (i) + 1);"
in .java and not in javascript!

I need to take out this from javascript because i'm going to implement into a filter which should execute this command but of course in java:

" crypted += String.fromCharCode (r.charCodeAt (i) + 1);
 
But you said you've already done is as

Code:
crypttext += (myaddr.charAt(i))+1;

What's wrong with this approach?

Cheers,
Dian
 
This: crypttext += (myaddr.charAt(i))+1; doesn't brings back exactly the same result as I get from javascript.

I think because that " String.fromCharCode" what I execute in javascript I don't do in java, but I don't know which method which is equivalent with it.
 
Take a look at the Character class.

Anyway, what differences are you getting? Maybe you're stting a diferent charset/encoding?

Cheers,
Dian
 
so for instance i have this test user with this test e-mail address: mailto:test.user(at)test.com

on the java side i get this result from the coding:
11098106109117112591171021161174711811610211541981174211710211611747100112110

and on the javascript i get this:
nbjmup;uftu/vtfs)vft*..
 
Code:
 String v = "asdas";
 int length = v.length();
 StringBuffer b = new StringBuffer();

 for ( int i = 0 ; i < length ; i++ ){
   b.append( (char) (v.charAt(i) + 1) );
 }
 System.out.println(b);

Tim
 
If you do use a StringBuffer like this, you may also want to add a slight improvement by telling it how much room to allocate ...
Code:
 String v = "asdas";
 int length = v.length();
 StringBuffer b = new StringBuffer([b]length[/b]);

 for ( int i = 0 ; i < length ; i++ ){
   b.append( (char) (v.charAt(i) + 1) );
 }
 System.out.println(b);

Tim
 
It's strange, the += option works ok for me.

Code:
char c='a';
String b="Hola ";
c++;
b+=c;
System.out.println(b);

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top