admin管理员组

文章数量:1291037

We must do a small program for our teacher to get the ASCII code of any value in Javascript.

I have searched and researched, but it seems that there is no method to do so. I have only found:

charCodeAt()

.html

That returns the Unicode value, but not ASCII.

I have read in this forum that the ASCII value is the same as the Unicode value for the ASCII characters that already have an ASCII value:

Are Unicode and Ascii characters the same?

But it seems that is not always the case, as for example with the extended ASCII characters. So for example:

var myCaracter = "├";

var n = myCaracter.charCodeAt(0);

document.write (n);

The ASCII value of that character is 195, but the program returns 226 (Unicode value).

I can't find a pattern to follow to convert from one to another, so:

¿Can we obtain the ASCII from Unicode, or should I look for another way?

Thanks!

We must do a small program for our teacher to get the ASCII code of any value in Javascript.

I have searched and researched, but it seems that there is no method to do so. I have only found:

charCodeAt()

http://www.hacksparrow./get-ascii-value-of-character-convert-ascii-to-character-in-javascript.html

That returns the Unicode value, but not ASCII.

I have read in this forum that the ASCII value is the same as the Unicode value for the ASCII characters that already have an ASCII value:

Are Unicode and Ascii characters the same?

But it seems that is not always the case, as for example with the extended ASCII characters. So for example:

var myCaracter = "├";

var n = myCaracter.charCodeAt(0);

document.write (n);

The ASCII value of that character is 195, but the program returns 226 (Unicode value).

I can't find a pattern to follow to convert from one to another, so:

¿Can we obtain the ASCII from Unicode, or should I look for another way?

Thanks!

Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked Oct 12, 2016 at 21:44 user6791921user6791921 23
  • 1 differencebetween/technology/web-applications/… – Blorgbeard Commented Oct 12, 2016 at 21:47
  • 2 "The" ASCII value is not a definite given for ... well, what es down to non-ASCII characters. I'm betting charCodeAt was what your teacher was after. – Jongware Commented Oct 12, 2016 at 21:52
  • 2 ASCII is a 7-bit code, so there's no character 195. Extended ASCII is a name for a group of many 8-bit codes. There is no single accepted 8-bit "ASCII" code. – Pointy Commented Oct 12, 2016 at 21:54
  • 1 If you are asking for "extended ASCII characters", then you need to meticulously describe which extension you mean. After all, Unicode is just another extension of ASCII. – Bergi Commented Oct 12, 2016 at 21:58
  • 3 Ask your teacher to e here and tell us what is the difference between the ASCII code and the Unicode code point for an ASCII character. charCodeAt definitely returns the ASCII code for any ASCII character. No doubt about it. You can actually try it with any ASCII character, you'll find the list here: en.wikipedia/wiki/ASCII – jcaron Commented Oct 12, 2016 at 22:12
 |  Show 18 more ments

2 Answers 2

Reset to default 4

ASCII characters only use 7 bits, with values from 0 to 127 (00 to 7F hex). They include:

  • control characters (0 to 31, as well as 127)
  • digits (0 to 9, encoded 48 to 57)
  • uppercase letters (65 to 90)
  • lowercase letters (97 to 122)
  • a limited number of punctuation and other symbols.

ASCII characters are a subset of Unicode (the "C0 Controls and Basic Latin Block"), and they are encoded exactly the same in UTF-8. The ASCII code of "A" (65 or 0x41) is the same as the Unicode code point for "A" (U+0041).

The character () you're considering is not ASCII. It's part of many different character sets / code pages, where it may have different numerical values / encodings, but it's definitely not ASCII.

That characters is not even defined in the most mon ASCII 8-bit extensions, known as ISO-8859-*. It is part of the code page 437 (used on MS-DOS), where its numerical code is 0xC3 (195). But that's definitely not ASCII.

The Unicode code point for that character is U+251C (9500 decimal), which is the return value of charCodeAt for this character, not 226.

You're probably getting 226 because you're interpreting an UTF-8 string that has not been recognised as such.

Today my teacher has apologized because maybe it was her fault to tell us that charCodeAt() is wrong to obtain the ASCII code; she wanted us to use that method, like @Rad Lexus suggested.

So, it is not neccesary in my excercise, but as a practice and to help everyone who could need it, what I have done is to add to the code a small validation in order to avoid that the user could enter ASCII extended characters bigger than or equal to 128, where the problems with charCodeAt() seem to start.

Maybe it is not a smart solution and it was certainly not necessary in my exercise, plus it makes that some necessary characters in another languages (ö for German or ñ for Spanish, for example) are forbidden... but I think it is good to post the code and let everyone which uses it to choose whether using this validation or not.

Thanks to everyone who helped me.

Defining function:

function validate(text)
{

    var isValid=false;

    var i=0;


    if(text != null && text.length>0 && text !='' )
    {
        isValid=true;

        for (i=0;i<text.length;++i)/*this is not necessary, but I did*/
        {
            if(text.charCodeAt(i)>=128)
            {
                isValid=false;
            }
        }

    }

    return isValid;

}

Using function

var isValid=false;

var position=0; 

while(isValid==false)
{
    text=prompt("Enter your text");

    isValid=validate(text);
}

本文标签: stringCan we convert Unicode to ASCII in Javascript charCodeAt() is only for UnicodeStack Overflow