Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27013 > unrolled thread

Xor characters in string do not work

Started byjonas.thornvall@gmail.com
First post2015-03-11 04:44 -0700
Last post2015-03-11 07:52 -0700
Articles 6 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  Xor characters in string do not work jonas.thornvall@gmail.com - 2015-03-11 04:44 -0700
    Re: Xor characters in string do not work Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-11 13:09 +0000
      Re: Xor characters in string do not work jonas.thornvall@gmail.com - 2015-03-11 06:49 -0700
        Re: Xor characters in string do not work Martin Honnen <mahotrash@yahoo.de> - 2015-03-11 15:34 +0100
          Re: Xor characters in string do not work Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-12 15:43 +0100
        Re: Xor characters in string do not work Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-11 07:52 -0700

#27013 — Xor characters in string do not work

Fromjonas.thornvall@gmail.com
Date2015-03-11 04:44 -0700
SubjectXor characters in string do not work
Message-ID<6485665e-6cf2-44b3-9641-6d5cd629c473@googlegroups.com>
<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>

[toc] | [next] | [standalone]


#27015

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-03-11 13:09 +0000
Message-ID<87385bisue.fsf@bsb.me.uk>
In reply to#27013
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.

[toc] | [prev] | [next] | [standalone]


#27016

Fromjonas.thornvall@gmail.com
Date2015-03-11 06:49 -0700
Message-ID<8fdd532b-a5d9-41db-8f95-fd0975b2f77a@googlegroups.com>
In reply to#27015
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.

[toc] | [prev] | [next] | [standalone]


#27017

FromMartin Honnen <mahotrash@yahoo.de>
Date2015-03-11 15:34 +0100
Message-ID<mdpjpn$b2l$1@adenine.netfront.net>
In reply to#27016
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 ---

[toc] | [prev] | [next] | [standalone]


#27020

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-03-12 15:43 +0100
Message-ID<3767006.WovHSH5Fpe@PointedEars.de>
In reply to#27017
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/?filter=map>
 
> 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/unicode.js> 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.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27018

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-03-11 07:52 -0700
Message-ID<45210e25-5d69-4a21-b54f-12b5e5364720@googlegroups.com>
In reply to#27016
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web