By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,068 Members | 867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,068 IT Pros & Developers. It's quick & easy.

Converting a String object to individual bytes

P: n/a
Hi,

I need to access the individual bytes in a string in my javascript
function.
If the characters in the string were all ascii characters then there
would not be a problem, however in my case the string may contain
ISO-2022-JP or UTF8 characters, so these characters may be larger than
1 byte each.
Is there a way to extract the individual bytes, or convert a string to
an array of bytes in javascript?

Any suggestions would be great.

Thanks in advance,

Erinys.
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Erinys wrote:
Hi,

I need to access the individual bytes in a string in my javascript
function.
If the characters in the string were all ascii characters then there
would not be a problem, however in my case the string may contain
ISO-2022-JP or UTF8 characters, so these characters may be larger than
1 byte each.
Is there a way to extract the individual bytes, or convert a string to
an array of bytes in javascript?

Any suggestions would be great.

Thanks in advance,

Erinys.


<script type="text/javascript">

// test string
var s = 'your string' +
String.fromCharCode(1024, 1025, 1026, 1027) +
String.fromCharCode(65535);

// generate the byte array
var bytes = [];
for (var ii = 0; ii < s.length; ++ii) {
var c = s.charCodeAt(ii);
// reverse these, depending on how
// you want to store the results
bytes.push(c >>> 8, c & 255);
}

// output the results
for (var ii = 0; ii < bytes.length; ii += 2) {
document.write(
bytes[ii] + ',' +
bytes[ii + 1] + '<br>'
);
}

</script>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.