I really need a way to convert all characters from CP1251 table to ASCII codes from 0 to 255. The only way that I found till now is the charCodeAt() function which only works for codes up to 128. For the upper codes it issues an Unicode number which is not good for me.
Asked
Active
Viewed 3,843 times
0
-
Not sure I understand. Can you make an example of what such a conversion would look like? – Pekka Sep 02 '11 at 08:02
-
2ASCII codes only go from 0 to 127. There **are no ASCII codes > 127!** – Joachim Sauer Sep 02 '11 at 08:23
-
the problem is that I want to take the ASCII codes of the extended ASCII table CP1251. I'm getting this easyly with PHP with intval(ord($encData[$i])). I need the same with javascript. Here is the ASCII table that I'm using http://www.ascii.ca/cp1251.htm – jacksun Sep 02 '11 at 09:16
-
Are you searching for a `charCodeAt()` function that uses ANSI CP151 instead of Unicode? – Jens Mühlenhoff Sep 02 '11 at 14:45
-
1JS uses Unicode exclusively, you have to map the Unicode code point to the CP1251 character code using a translation table or something similar. – Jens Mühlenhoff Sep 02 '11 at 14:48
-
Also your question would be much easier to answer for us if you gave a code sample. – Jens Mühlenhoff Sep 02 '11 at 14:52
-
Jens Mühlenhoff you are the only one here that understands what I'm talking about. Thank you for your suggestion you are absolutelly correct. I've made already a portable solution using PHP but I'll have in mind what you said it's good idea. Thank you very much :) – jacksun Oct 06 '11 at 11:27
1 Answers
1
The first 128 characters in CP1251 are the same as the characters in ASCII. After that, they represent non-ASCII, cyrillic characters, which can't be converted to ASCII.
Consider using Unicode, which was invented to solve this kind of problem.

Thomas
- 5,736
- 8
- 44
- 67
-
this is not quite true because I did already with PHP but I need standalone solution that's why I'm trying to solve it with javascript. – jacksun Sep 02 '11 at 08:33
-
1Please tell me how you did it in PHP, because I do not see how it's possible given the basic definition of ASCII and CP1251. – Thomas Sep 02 '11 at 08:36
-
I can't use Unicode because I'm making decryption from ASCII encrypted table. The encryption is made by shifting the ASCII table. – jacksun Sep 02 '11 at 08:36
-
for ($i = 0; $i < strlen($encData); $i++) { $ascii = intval(ord($encData[$i])); //geting ascii code of the char from the string $ascii = $ascii - 33; //taking away useless ascii codes $ascii = 221 - $ascii; //reversing the encrypted ascii code $ascii = $ascii + 33; //adding again the useless ascii codes $newData .= chr($ascii); //adding char to string } – jacksun Sep 02 '11 at 08:36
-
this works perfectly for all CP1251 characters and issues codes from 0 to 255 and than when I'm making the reverse I'm converting back to chars. – jacksun Sep 02 '11 at 08:39
-
I'm not sure how to read that or what your code is doing there. And, as an aside, having comments after your statements is unconventional. – Thomas Sep 02 '11 at 08:43
-
I think there is some way to do this by converting to bytes or to HEX and then to ASCII but I'm not sure how to do it. Thank you for your answer if you come up with something I'll apreciate it :) – jacksun Sep 02 '11 at 08:44
-
intval(ord($encData[$i]) - this is how I'm getting the ASCII code the other code is making decryption and converting back to chars – jacksun Sep 02 '11 at 08:46
-
2I'm saying that it's impossible. How would you convert Ж to ASCII? Since ASCII is only 128 characters and Cyrillic is larger than that, the conversion is impossible. Let me know if you think I'm misunderstanding your questions. – Thomas Sep 02 '11 at 08:49
-
yeah you are right that ASCII is 128 characters but you forget that there is extended ASCII tables which is exactly CP1251. here you can find the table: http://www.ascii.ca/cp1251.htm – jacksun Sep 02 '11 at 08:55
-
1There is no ASCII 198 and whatever you're using to retrieve that number is wrong. Check http://en.wikipedia.org/wiki/Windows-1251, and http://en.wikipedia.org/wiki/ASCII for more information. I'm going to leave it at that, so I don't explode this comment section. Good luck. – Thomas Sep 02 '11 at 09:03
-
thank you but in links you gave me the ASCII code of Ж is 198: (Ж 0416 198) - from en.wikipedia.org/wiki/Windows-1251 the second link is about the standard ASCII codes upto 128. Thank you anyway :) – jacksun Sep 02 '11 at 09:08
-
@user902786: please avoid the user of the name "extended ASCII". It's *at best* a classification of single-byte encodings that are based on ASCII. But there is no *single* encoding that's called "extended ASCII". If you talk about Windows-1251, then call it Windows-1251. Don't call it "extended ASCII". – Joachim Sauer Sep 02 '11 at 12:37
-
@Joachim Sauer: ok I'm talking about Windows-1251. Can I get codes as shown at this table: ascii.ca/cp1251.htm with some kind of converting like I'm doing with PHP? Thanks in advice. – jacksun Sep 02 '11 at 12:44