473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Converting a String object to individual bytes

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
1 3337
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
8
by: iyuen | last post by:
I'm having problems with converting a byte array to an image object~ My byte array is an picture in VB6 StdPicture format. I've used propertybag to convert the picture into base64Array format in...
2
by: Claire | last post by:
I need to interface to an MFC CArchive file created with Visual Studio 6. I've found some online sample code to translate an archived string into C# I now need to read in and translate a CTime...
12
by: Mark Rae | last post by:
Hi, Can anyone please tell me how to convert an unserializeable object say, a System.Web.Mail.MailMessage object, to a byte array and then convert the byte array to a Base64 string? Any...
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
5
by: Testguy | last post by:
Hi, I was wondering if one the smart people that frequent this group could give me a hand with a small program I am attempting to debug. I am not a highly experienced developer, but can...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.