473,480 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

charCodeAt()

hi, sometimes I get weird codes by String.charCodeAt() if I'am converting
binary data (iso-8859-1 written by a java app) into numeric values. For
example instead of 164 I got 8364 (the generic currency symbol '¤'). I
have already tested it by the url-statement javascript:("¤").charCodeAt();
in my browser, the result is 164. Some other values exeeding the maximum
of 255 for the first latin-1 conform unicode set just only a bit, but I am
wondering that they're doing it at all.
regards
ron
Jul 23 '05 #1
7 5363


Ron Lange wrote:
sometimes I get weird codes by String.charCodeAt() if I'am
converting binary data (iso-8859-1 written by a java app) into numeric
values. For example instead of 164 I got 8364 (the generic currency
symbol '¤'). I have already tested it by the url-statement
javascript:("¤").charCodeAt(); in my browser, the result is 164. Some
other values exeeding the maximum of 255 for the first latin-1 conform
unicode set just only a bit, but I am wondering that they're doing it
at all.


String.charCodeAt() is a method on JavaScript strings and JavaScript
doesn't know binary data thus I am not sure what you are doing. I think
you need to provide some more information on what the Java app is doing
and how it interacts with JavaScript (in the browser?, or are you using
some JavaScript implementation like Rhino in a Java app?).
Strings in JavaScript are Unicode encoded since Netscape 4.06 and IE4 thus
"¤".charCodeAt()
should indeed yield
164
while you should see
8364
as the result of
"€".charCodeAt()

Thus if you are having any problems you want help with you need to
provide more information, if it is JavaScript in a browser tell us the
browser version you are using and how it interacts with that Java app.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
Hi Martin,
the application is writing numeric values as iso-8859-1 encoded characters
(further called binary data;-). These characters are concatenated to one
string var within a javascript in a simple html page, which is then
converted back into numeric values by this script.

And, to prevent further detail questions ;-), of course the javascript
literals quote, newline and backslash are written as octal escape sequence.

In my little computer scientist's brain I assumed something like that a
iso-8859-1 encoded string should be treated as iso-8859-1 string, even the
charCodeAt() method is defined on Unicode or whatever, since the
iso-8859-1 literals should be the lowest 8-bit set. For safety I
determined the document's charset in the html page, too.

Additional information:
Used Browsers: Opera 7.53, Opera 7.52 and Mozilla 1.4 on Solaris and Linux

Regards
Ron
Jul 23 '05 #3
I put a java generated page on the net to illustrate my issue.

http://home.teleos-web.de/rlange1/chartest.htm
Jul 23 '05 #4


Ron Lange wrote:

the application is writing numeric values as iso-8859-1 encoded
characters (further called binary data;-). These characters are
concatenated to one string var within a javascript in a simple html
page, which is then converted back into numeric values by this script.
So the Java application is some JSP or servlet or other thing answering
HTTP requests of a browser and sending it a HTTP response, in this case
a HTML page with some JavaScript section embedded, is that right?
And, to prevent further detail questions ;-), of course the javascript
literals quote, newline and backslash are written as octal escape sequence. In my little computer scientist's brain I assumed something like that a
iso-8859-1 encoded string should be treated as iso-8859-1 string, even
the charCodeAt() method is defined on Unicode or whatever, since the
iso-8859-1 literals should be the lowest 8-bit set. For safety I
determined the document's charset in the html page, too.
If it is a HTML page then the browser/user agent will decode that whole
page by trying to use any encoding send in the HTTP response (e.g. in the
Content-Type: text/html; charset=ISO-8859-1
header)
or if nothing is present there then by looking for a
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
element line in the <head> section of the HTML page.
So whatever your Java app writes in that JavaScript section in the whole
HTML page being sent should match the encoding for the whole page, then
I don't see any problems, the whole script should be passed on to the
script engine in whichever encoding that prefers internally and should
work with Unicode strings.
What happens if you do a view-source of the page your Java app sends, if
you look at the JavaScript string literal in the source, which
characters show up there?
What happens if you do a document.write of the JavaScript string
literal, which characters show up there?
I suspect that you should already see something there that you do not
expect, I have some doubts that the problem is the charCodeAt method if
two different browsers (as stated below) give you the same result.
Additional information:
Used Browsers: Opera 7.53, Opera 7.52 and Mozilla 1.4 on Solaris and Linux


If your app is online post a URL and someone in the newsgroup can then
examine the HTML/JavaScript send by it to find out what is going wrong.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #5
Hi Martin,
no, it is no cgi. Please have a look at

http://home.teleos-web.de/rlange1/chartest.htm

where you can find an illustration of my problem. I forget to mention that
different browsers obvisiously threat the encoding in different ways
(although page encoding should be preserved).

Regards and thank you for your reply
Ron

Jul 23 '05 #6
Ron Lange wrote:
hi, sometimes I get weird codes by String.charCodeAt()
You mean String.prototype.charCodeAt().
if I'am converting binary data (iso-8859-1 written by a java app)
You are confusing JavaScript and Java:

<http://jibbering.com/faq/#FAQ2_2>
into numeric values. For example instead of 164 I got 8364
(the generic currency symbol '¤').
I do not. Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6)
Gecko/20040122 Debian/1.6-1, using a JavaScript 1.5 script
engine and de_DE@euro as locale (where @euro is equal to
@ISO-8859-15 AFAIK).

The script engine you have tested with seems to try to
workaround the Euro issue, possibly due to system limitations.
In ISO-8859-1, code point 0xA4 (164) is the Currency sign. In
ISO-8859-15, that code point is the Euro sign. In Unicode,
however, the Euro sign is located at code point 0x20AC (8364).
I have already tested it by the url-statement
javascript:("¤").charCodeAt();
There is nothing like a "url-statement". What you have used is a
proprietary "javascript:" URI designed to return a dynamic document
generated from JavaScript expressions. By using a call operator
within the expression, the respective method is called (and its
return value, if it has any, creates a temporary document).

<http://jibbering.com/faq/#FAQ4_24>
in my browser,
Which is? On which OS? On which platform?

<http://jibbering.com/faq/#FAQ2_3>
the result is 164.
Seems like your browser's script engine, too, is interpreting strings with
charCodeAt() as ISO-8859-1 strings (where appropriate) and not as Unicode
strings.

But then your test case is not even Valid HTML:

<http://validator.w3.org/check?uri=http://home.teleos-web.de/rlange1/chartest.htm&ss=1;verbose=1>
Some other values exeeding the maximum of 255 for the first latin-1
conform unicode set
There is no "Latin-1 conform unicode set". Latin-1 (ISO-8859-1) is a subset
of Unicode, with the exception of a few code points, including 0xA4 (164).
The correct designation for the unicode subsets without those exceptions
is "Basic Latin" and "Latin-1 Supplement".

<http://www.unicode.org/charts/>
<http://www.htmlhelp.com/reference/charset/iso160-191.html>
just only a bit, but I am wondering that they're doing it at all.


See also <40**************@PointedEars.de>.
PointedEars
Jul 23 '05 #7
Am Sun, 25 Jul 2004 22:26:27 +0200 hat Thomas 'PointedEars' Lahn
<Po*********@web.de> geschrieben:
if I'am converting binary data (iso-8859-1 written by a java app)
You are confusing JavaScript and Java:


No, I don't confuse it, just follow the thread.
There is nothing like a "url-statement". What you have used is a
proprietary "javascript:" URI ---snip---


Thank you very much. I'm wondering why someone is spending so much energy
on explaining such trivial things. But anyway. The problem couldn't be
solved since just only the first 128 values of all iso charsets being
interpreted in the same way by charCodeAt(), there I was a bit
disinformed. Finally, the capabilities of JavaScript for e.g. high
compressed content are quite unsatisfying.

And: be nice and dont' reply, this thread is terminated.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3204
by: Bern | last post by:
msdn docs say the String.charCodeAt method is supported by IE 5.5 and above. I'm just curious that what do people use before IE 5.5 was released? charCodeAt seems to be such an important method.
5
7635
by: ken | last post by:
hello, i'm using charCodeAt to get the ascii value at a given location in a string but now i want to set the value of a location in a string to a specific ascii value. is there a way to do this?...
8
1997
by: Senderos | last post by:
Hello, i try to break this code, since many days (and nights), without any success : <script type="text/javascript"> function p(a,i,c) { var x=a.charCodeAt(i)+c;
1
2448
by: simmchip | last post by:
does anyone know how to complete the following,im trying to write a function that prompts for a message, and encode the message using shift cipher. mine does not work for some reason? change = 0...
10
8674
by: czechboy | last post by:
Hi, I would like to use charCodeAt function but it returns wrong dec. numbers. The question is how to set character of the js. file executed? I can not use any kind of tags (<script<metaetc) since...
1
2018
by: Mike | last post by:
Hello, Is there a reverse function to string.charCodeAt(index);? Thanks Mike
2
14654
by: ziycon | last post by:
Is there a function that does the opposite of charCodeAt(), I've writing a basic password tool and i can turn the text into unicode no problem but I can't return the unicode back to text?
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.