C functions to Javascript
C functions to Javascript
I'm trying to convert 2 functions from C to Javascript, but I'm failing on that.
The C functions im trying to convert is:
void encrypt(char password,int key)
unsigned int i;
for(i=0;i<strlen(password);++i)
password[i] = password[i] - key;
void decrypt(char password,int key)
unsigned int i;
for(i=0;i<strlen(password);++i)
password[i] = password[i] + key;
The C functions is from: http://c-program-example.com/2012/04/c-program-to-encrypt-and-decrypt-a-password.html
What i did in Javascript is this:
var password = "hello"
var key = 322424;
for (var i=0; i<password.length; i++)
var char = password[i].charCodeAt();
var s = String.fromCharCode(char-key);
alert(s);
I'm putting the alert to see if it working correctly before making them as functions. Can someone please show me how it is done correctly in Js?
charCodeAt(i)
I'm quite new into javascript, could you please provide a working sample
– John
Sep 12 '18 at 13:27
Tip:
encrypt is the same as decrypt with -key.– Eugene Sh.
Sep 12 '18 at 13:29
encrypt
decrypt
-key
javascript doesn't have the same concept of types as C, so when you subtract 322424 from
char, you're going to get a very negative number– Chris Turner
Sep 12 '18 at 13:31
char
Re. "how can i make it like C?" ; after each arithmetic operation, mask out the upper bits so the result is modulo-256 (e.g.
(char - key) & 0xff). I cannot post an answer because I am no JavaScript expert, and that is by no means the only issue.– Clifford
Sep 12 '18 at 14:10
(char - key) & 0xff
2 Answers
2
Try this, as said by Clifford in the comments, each char code has to be masked to modulo-256 because the size of char. Also, since decrypt is the same but substracting key, I reused encrypt but with a negative key.
char
decrypt
key
encrypt
key
Here you have a working snippet, let me know if it gives the desired output.
function encrypt (password, key)
var i,
output = '';
for (i = 0; i < password.length; i++)
var charCode = password.charCodeAt(i),
keyedCharCode = (charCode - key) & 0xff;
output += String.fromCharCode(keyedCharCode);
return output;
function decrypt (password, key)
return encrypt(password, -key);
var password = 'hello',
key = 322424,
encrypted = encrypt(password, key),
decrypted = decrypt(encrypted, key);
console.log('encrypt', encrypted);
console.log('decrypt', decrypted);
Thank you that worked! :-)
– John
Sep 12 '18 at 18:04
Glad it worked. Just as a an advice if you come here again with another transpilation question, provide examples of inputs and its expected output along with the code, that way we have something to test with when answering. There are a lot of differences between JS and C, not just syntax, that can be easily overlook.
– Alvaro Castro
Sep 12 '18 at 19:01
A char in C is actually a tiny integer, which value in hexa goes from 00 to FF. char is signed in many implementations (unsigned char is not), so its values go from -128 to 127 (256 values total, or 0 to 255 if unsigned). An array of chars, char is an array of tiny integers, conventionally the last char of a 'string' in C is the byte 0x00 that marks the end (for instance, printf knows the end is there when the byte 0 is met).
char
00
FF
char
unsigned char
-128
127
0
255
char
printf
0
In Javascript a string is a set of Unicode characters.
In C,
char s = "hello";
int k = 322424;
char *t = s;
while(*t) *t++ -= k; // 't' is the encoded version of 's'
// print the integer values of the new encoded 'string'
for(t=s ; *t ; t++) printf("%d ", *t); // -16 -19 -12 -12 -9
// (unsigned: 240 237 244 244 247, or add 256 to negative values above)
printf("n");
A char cannot contain more than a byte.
char
In Javascript, you have to keep the resulting number in a byte-range, otherwise, a multi-byte character might be stored instead. For instance
String.fromCharCode(0x3042) gives "あ" (Unicode [character 0x3042](https://www.key-shortcut.com/en/writing-systems/%E3%81%B2%E3%82%89%E3%81%8C%E3%81%AA-japanese/))
In C,
char c = 0x3042; // will only keep one byte, the LSB, 0x42
Thus in Javascript, try something like
var s = "hello";
var k = 322424;
var i,l = s.length;
var t = '';
// Note the `& 0xff` that keep the result within a byte range
for(i=0 ; i<l ; i++) t += String.fromCharCode((s.charCodeAt(i) - k) & 0xff);
// 't' is the result, let see its char codes
var u = '';
for(i=0 ; i<l ; i++) u += t.charCodeAt(i) + ' ';
console.log(u); // 240 237 244 244 247 (same as in C, 240-256 = -16 ...)
The JS char codes are unsigned, thus the positive results. They're the same as in C.
Whether
char is sugned or unsigned in C is implementation defined. Adding 256 to a char value -16 in C will only result in 240, if assigned to a larger type - when assigned to a char the will remain -16. It has no effect on the bit pattern. Simpler to use explicit unsigned char or cast to unsigned char - but it makes no difference to the character the code is associated with; -16 and 240 refer to the same char bit pattern on platforms where char is 8 bit (again implementation defined), and therefore the same character.– Clifford
Sep 12 '18 at 14:26
char
char
unsigned char
unsigned char
char
char
@Clifford
char signedness is implementation dependent, had to be said (and was added). Besides, I don't think the answer was missing the rest, did it?– Ring Ø
Sep 12 '18 at 14:48
char
char c = 0x3042; // will only keep one byte, the LSB, 0x42 Actually the C standard says the result is either an implementation defined value or an implementation defined signal, so in principle this could segfault (raise SIGSEGV).– melpomene
Sep 12 '18 at 17:20
char c = 0x3042; // will only keep one byte, the LSB, 0x42
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Use
charCodeAt(i)– Luca Kiebel
Sep 12 '18 at 13:26