[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Xor characters in string do not work

JT

3/11/2015 11:45:00 AM

<SCRIPT LANGUAGE="Javascript">
key="nyckel";
cryptstr="text"
j=1;
i=1;
function test(){
document.write(key.charCodeAt[j]);
document.write(cryptstr.charCodeAt[j]);
cryptstr.charCodeAt[i]^= key.charCodeAt[j]^cryptstr.charCodeAt[i];
document.write(cryptstr[i]);
}
test();
</script>
6 Answers

ram

3/11/2015 12:21:00 PM

0

jonas.thornvall@gmail.com writes:
>cryptstr.charCodeAt[i]^= key.charCodeAt[j]^cryptstr.charCodeAt[i];

xor o xor = id

Ben Bacarisse

3/11/2015 1:09:00 PM

0

jonas.thornvall@gmail.com writes:

XOR does work but...

> <SCRIPT LANGUAGE="Javascript">
> key="nyckel";
> cryptstr="text"
> j=1;
> i=1;

You'd be better off using var to declare these variables inside the
function "test" (though obviously you'll want to use parameters eventually).

> function test(){ document.write(key.charCodeAt[j]);

charCodeAt is a function -- use key.charCodeAt(j).

> document.write(cryptstr.charCodeAt[j]);

Ditto.

> cryptstr.charCodeAt[i]^= key.charCodeAt[j]^cryptstr.charCodeAt[i];

You can't assign to the result of charCodeAt. You can't even modify a
string using the special cryptstr[i] syntax. You need to build a new
string from the old one.

Finally, two ^ operations (as are hinted at here) are a no-op. I.e. an
XOR operation with some value is its own inverse.

> document.write(cryptstr[i]);
> }
> test();
> </script>

--
Ben.

JT

3/11/2015 1:49:00 PM

0

Den onsdag 11 mars 2015 kl. 14:09:23 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> XOR does work but...
>
> > <SCRIPT LANGUAGE="Javascript">
> > key="nyckel";
> > cryptstr="text"
> > j=1;
> > i=1;
>
> You'd be better off using var to declare these variables inside the
> function "test" (though obviously you'll want to use parameters eventually).
>
> > function test(){ document.write(key.charCodeAt[j]);
>
> charCodeAt is a function -- use key.charCodeAt(j).
>
> > document.write(cryptstr.charCodeAt[j]);
>
> Ditto.
>
> > cryptstr.charCodeAt[i]^= key.charCodeAt[j]^cryptstr.charCodeAt[i];
>
> You can't assign to the result of charCodeAt. You can't even modify a
> string using the special cryptstr[i] syntax. You need to build a new
> string from the old one.
>
> Finally, two ^ operations (as are hinted at here) are a no-op. I.e. an
> XOR operation with some value is its own inverse.
>
> > document.write(cryptstr[i]);
> > }
> > test();
> > </script>
>
> --
> Ben.

Bem is there really no function that convert a string into an array of ascii 8 bit values?

Do i really need to loop with charCodeAt building up the string?
Or is there better way

for (i=0;i<key.length;i++) keyarr[i]=key.charCodeAt(i);

This seem a bit primitive is there no other way to simply remap the characters from a full string into an array.

Let me dream a little keyarr=key.charCodeStr()
Voila it is done.

Martin Honnen

3/11/2015 2:35:00 PM

0

jonas.thornvall@gmail.com wrote:
> Den onsdag 11 mars 2015 kl. 14:09:23 UTC+1 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:


> Do i really need to loop with charCodeAt building up the string?
> Or is there better way
>
> for (i=0;i<key.length;i++) keyarr[i]=key.charCodeAt(i);
>
> This seem a bit primitive is there no other way to simply remap the characters from a full string into an array.
>
> Let me dream a little keyarr=key.charCodeStr()
> Voila it is done.

You can (at least in JavaScript) treat a string like an array and use
the "map" method:

var s = "abc";
var a = [].map.call(s, function(ch) { return ch.charCodeAt(); });


But of course strings are sequences of 16 bit Unicode characters not of
8 bit values.


--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Scott Sauyet

3/11/2015 2:52:00 PM

0

jonas.thornvall@gmail.com wrote:

> for (i=0;i<key.length;i++) keyarr[i]=key.charCodeAt(i);
>
> This seem a bit primitive is there no other way to simply remap the
> characters from a full string into an array.

Well, you can turn a string into an array as follows:

key.split(''); // 'nyckel' => ['n', 'y', 'c', 'k', 'e', 'l']

Then you can use `Array.prototype.map` on the resulting array to transform
it into a new array.

Or, as Martin says, you can apply `map` directly to the string.


-- Scott

Thomas 'PointedEars' Lahn

3/12/2015 2:44:00 PM

0

Martin Honnen wrote:

> jonas.thornvall@gmail.com wrote:
>> Do i really need to loop with charCodeAt building up the string?
>> Or is there better way
>>
>> for (i=0;i<key.length;i++) keyarr[i]=key.charCodeAt(i);

More efficient â??forâ? loops are written this way:

for (var i = 0, len = key.length; i < len; ++i)
{
// â?¦
}

>> This seem a bit primitive is there no other way to simply remap the
>> characters from a full string into an array.
>>
>> Let me dream a little keyarr=key.charCodeStr()
>> Voila it is done.
>
> You can (at least in JavaScript) treat a string like an array and use
> the "map" method:

Array.prototype.map() is supported by several implementations of ECMAScript
Ed. 5 and later:

<http://PointedEars.de/es-matrix/?filt...

> var s = "abc";
> var a = [].map.call(s, function(ch) { return ch.charCodeAt(); });

or

var a = s.split("").map(function (ch) { return ch.charCodeAt(); });

> But of course strings are sequences of 16 bit Unicode characters not of
> 8 bit values.

No, String values are, unfortunately, defined in ECMAScript (up to including
the 5.1 Edition) as â??sequences of 16-bit unitsâ? only. Therefore, the built-
in String properties do not work for Unicode characters beyond the Basic
Multilingual Plane (BMP).

Remedy:
<http://PointedEars.de/wsvn/JSX/trunk/string/unic... and the like.

> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---

Eeek. Must you use *that*? It is a known source of trolls.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.