472,804 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

how do you convert a string to an integer array?


I know how to conver a string to an array of strings, but I need to convert an ascii string to an
array of integers (really unsigned chars). Eg,

$str ="ABC";

needs to convert to something like this:

$buf = array(0x41, 0x42, 0x43);

Anyone know how? I haven't been able to find a way.

David

Jul 17 '05 #1
4 6016
David Lawson <da**********@xdevnullx.com> wrote:
$str ="ABC";

needs to convert to something like this:

$buf = array(0x41, 0x42, 0x43);

Anyone know how? I haven't been able to find a way.


See the string functions: http://www.php.net/manual/en/function.ord.php
and convert those values to hex, of use
http://www.php.net/manual/en/function.bin2hex.php and some (regexp)
magic.

Jul 17 '05 #2
On Wed, 30 Mar 2005 16:26:13 -0500, David Lawson wrote:
I know how to conver a string to an array of strings,
but I need to convert an ascii string to an
array of integers (really unsigned chars).


$s = 'ABC';
$a = array_map('ord', preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY));

Or for PHP5:

$a = array_map('ord', str_split($s));

Gives:

$a => Array
(
[0] => 65
[1] => 66
[2] => 67
)

--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3
On Wed, 30 Mar 2005 23:48:53 +0200, Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
On Wed, 30 Mar 2005 16:26:13 -0500, David Lawson wrote:
I know how to conver a string to an array of strings,
but I need to convert an ascii string to an
array of integers (really unsigned chars).


$s = 'ABC';
$a = array_map('ord', preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY));

Or for PHP5:

$a = array_map('ord', str_split($s));

Gives:

$a => Array
(
[0] => 65
[1] => 66
[2] => 67
)


thanks,
$a = array_map('ord', str_split($s));
did what I needed.

Now I have a binary (integer) array and need to write it to a file.

I've been experimenting with

pack, fwrite, and file_put_contents but can't get any of these to work.

Do I need to convert the (modified) integer array back to a binary string first?
I've tried pack():

$bstr = pack("C*", $buf2); // convert buf2 to a binary string
file_put_contents($this->tmpname, $bstr);

This only writes 1 byte to a file. The count of $buf2 is 256.

David

Jul 17 '05 #4
On Thu, 31 Mar 2005 10:18:39 -0500, David Lawson <da**********@xdevnullx.com>
wrote:
On Wed, 30 Mar 2005 23:48:53 +0200, Ewoud Dronkert <fi*******@lastname.net.invalid> wrote:
$a = array_map('ord', str_split($s));

Gives:

$a => Array
(
[0] => 65
[1] => 66
[2] => 67
)


thanks,
$a = array_map('ord', str_split($s));
did what I needed.

Now I have a binary (integer) array and need to write it to a file.


Well, you have an array of integers. Not "unsigned chars" as you wanted
..I've been experimenting with

pack, fwrite, and file_put_contents but can't get any of these to work.

Do I need to convert the (modified) integer array back to a binary string first?
I've tried pack():

$bstr = pack("C*", $buf2); // convert buf2 to a binary string
file_put_contents($this->tmpname, $bstr);

This only writes 1 byte to a file. The count of $buf2 is 256.


Er, let's back up a step here. Unlesss I've missed some subtlety here, you
said:
$str ="ABC";

needs to convert to something like this:

$buf = array(0x41, 0x42, 0x43);


And you want to write that to a file. Well, just write "ABC". It's the same
thing. It sounds like you've gone full-circle from having a string, turning it
into an array of integer codes that each character is represented by, to
writing back those integers as bytes - when exactly the same effect is given by
just writing the string in the first place..?

In fact by going via ord() you're entering the world of character set
encodings; ord() only claims to handle ASCII which is 0-127. Although you did
mention ASCII originally so you're probably safe.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5

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

Similar topics

3
by: Tome73 | last post by:
How would I convert this string to an Array? "243, 567, 324, 345" Where each number becomes a new element in that Array. Thanks
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
43
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
3
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hi, I am looking for the best way to convert a string of length 1 (= 1 character as string) to integer that has the same value as numeric representation of that character. Background: I am...
5
by: Dave Bootsma | last post by:
I have an application where I want to redraw a polygon from points I retrieve from a file. The file is read with streamreader line by line each line contains the points for each polygon. Below is...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.