473,407 Members | 2,306 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,407 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 6086
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. ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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...
0
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
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,...

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.