473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3346
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.fromChar Code(1024, 1025, 1026, 1027) +
String.fromChar Code(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*****@agrico reunited.com>
comp.lang.javas cript 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
13902
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
25
6653
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 bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to...
3
2330
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 need to be converted to an enum. The next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct with the data. How does one do this...
8
4544
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 XML, and embedded the array as some child element in an xml file, i.e.: ...
2
2322
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 object. This is written to the file as 4 bytes. Can anyone tell me the structure please (It's a shame that Microsoft don't publish information on...
12
7724
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 assistance gratefully received. Best regards,
32
12485
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 Set wsName = DBEngine.Workspaces(0) Set dbName = wsName.OpenDatabase(CurrentProject.FullName) Set rsName = dbName.OpenRecordset("SQL Statement")
5
10314
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 create small programs. I was asked to write a small application that would look in a folder, and convert all the CSV files to XLS.
2
11574
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 void loadBytes(InputStream is){ DataSource ds=new DataSource(is); player = Manager.createPlayer(ds);
0
7734
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7826
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3512
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1960
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 we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
781
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.