-1

What would be a good way to turn a string into a series of digits in Javascript (I'm not talking about converting "0.5" into 0.5, but more "Hello" into 47392048)?

Any idea appreciated.

Thanks!

nkkollaw
  • 1,947
  • 1
  • 19
  • 29
  • 7
    And how does the number `47392048` relate to the word `Hello`? – David Thomas Apr 18 '13 at 20:52
  • what does 47392048 means in term of "hello" ? – Adidi Apr 18 '13 at 20:52
  • 1
    Figure out how to do it on paper, and then do the math using js. – What have you tried Apr 18 '13 at 20:53
  • 1
    I was thinking you meant something like this: http://www.mattkoenigphotography.com/wp-content/uploads/2010/10/Calculator_101011_2161-2.jpg :) – Jason Sperske Apr 18 '13 at 20:53
  • possible duplicate of [fastest MD5 Implementation in JavaScript](http://stackoverflow.com/questions/1655769/fastest-md5-implementation-in-javascript) – raina77ow Apr 18 '13 at 20:54
  • One way I thought would be to use character codes. For instance, using this: http://www.asciitable.com/. 'a' would be '97', 'b' '98', etc., but was wondering if there was a clever way to do that. – nkkollaw Apr 18 '13 at 20:55
  • 3
    If your string consists of just alphanumeric, case-insensitive characters, you could use `parseInt` with base-36: `parseInt('Hello', 36) === 29234652`. – Rob W Apr 18 '13 at 20:55
  • 1
    Could you mean using `String.charCodeAt()` https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/charCodeAt ? – Jason Sperske Apr 18 '13 at 20:55
  • @nbrogi, this solution is not viable if you do no separate segment to distinguish characters. – Boris Guéry Apr 18 '13 at 20:55
  • @raina77ow Sorry, I fail to see how md5 is even remotely relevant for this question. – Rob W Apr 18 '13 at 20:55
  • @nbrogi, maybe you should tell us what you're trying to achieve to help you to find the clever way to do it. – Boris Guéry Apr 18 '13 at 20:56
  • @JasonSperske That's how my obfuscator from [that question](http://stackoverflow.com/a/16036784/1249581) works :) – VisioN Apr 18 '13 at 20:56
  • 1
    @RobW I thought the question is about making a (stable) digest for any given string. For this case MD5 is quite suitable, imo. And yes, one can get a pure number by `parseInt`-ing the result. – raina77ow Apr 18 '13 at 20:56
  • @VisioN, no WAY! and I thought I was close to understanding it :P – Jason Sperske Apr 18 '13 at 20:58
  • @Rob: I love the simplicity of your suggestion, could you explain why base 36, though? (I sort of learned JavaScript by accident, so I don't have quite the insight into some JS that my rep, occasionally, suggests I should...) – David Thomas Apr 18 '13 at 20:58
  • @raina77ow: I don't know if md5 would work, it's not really a series of digits... – nkkollaw Apr 18 '13 at 20:59
  • @DavidThomas Base 36 is used because it can parse all small latin characters + digits. – VisioN Apr 18 '13 at 20:59
  • @RobW, this solution will produce unpredictable result, there is no way to distinguish uppercase and lowercase, and it won't be possible to revert properly. – Boris Guéry Apr 18 '13 at 21:00
  • @JasonSperske Yeah, and that's why it accepts only small latins and digits (+ spaces embedded additionally). – VisioN Apr 18 '13 at 21:01
  • 1
    @nbrogi, a md5 hash, or whatever hash, is often represented as an hexadecimal number, but nothing prevents you to convert it to base 10. – Boris Guéry Apr 18 '13 at 21:01
  • @RobW: that would work! – nkkollaw Apr 18 '13 at 21:03
  • @Boris Guéry: true—that would definitely work, too. – nkkollaw Apr 18 '13 at 21:04
  • @nbrogi, note that you won't be able to revert it. – Boris Guéry Apr 18 '13 at 21:06
  • @Boris Guéry: yes, I'm starting to think it's harder than I thought. I thought there would be something that was part of the language or trivial that could be implemented using built-in functions. Oh well... – nkkollaw Apr 18 '13 at 21:10
  • @nbrogi, explain what you are trying to achieve, we may find a better solution. – Boris Guéry Apr 19 '13 at 09:55
  • @Boris: nothing in particular. Just trying to figure out if it was possibile. – nkkollaw Apr 19 '13 at 14:13

2 Answers2

2

You can use the ASCII value of each letter:

"a letter".charCodeAt(0);
0

Ok, so given your comments, here is a (not widely tested) solution.

var str = "κόσμε 这是一条狗 é €";

$('#orig').after('<dd>' + str + '</dd>');

var result = "";
for (var i = 0, len = str.length, code, paddedCode; i < len; ++i) {
  code = str[i].charCodeAt(0).toString();
  paddedCode = code.length >= 8
    ? code
    : new Array(8 - code.length + 1).join(0) + code; result += paddedCode;
  result += paddedCode;
}

$('#nums').after('<dd>' + result + '</dd>');

var segments = result.match(/.{8}/g);

$.each(segments, function(k, v) {
    $('#nums-segmented').after('<dd>' + v + '</dd>');
});

revertedString = '';

for (var i = 0, len = segments.length; i < len; i=i+2) {
  revertedString += String.fromCharCode((segments[i] | 0));
}

$('#string').after('<dd>' + revertedString + '</dd>');

Run it at JSFiddle

The trick is to pad number and work with them as string when needed.

Boris Guéry
  • 47,316
  • 8
  • 52
  • 87
  • That makes perfect sense. I guess you still have to know the encoding, that would be the only problem with the approach. Thanks! Great work! – nkkollaw Apr 19 '13 at 20:18