FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » converting numbers to ascii values
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
converting numbers to ascii values [message #183889] Sun, 24 November 2013 16:58 Go to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
In BASIC I would run a for loop to print out the corresponding characters
for a given value.

for x=65 to 90
print chr$(x)
next x

would give ABCDEF......

how is this done in php?
Re: converting numbers to ascii values [message #183890 is a reply to message #183889] Sun, 24 November 2013 17:06 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
<noreply(at)example(dot)com> wrote:

> In BASIC I would run a for loop to print out the corresponding characters
> for a given value.
>
> for x=65 to 90
> print chr$(x)
> next x
>
> would give ABCDEF......
>
> how is this done in php?

for ($x = 65; $x <=90; ++$x)
printf("%c",$x);


HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: converting numbers to ascii values [message #183891 is a reply to message #183889] Sun, 24 November 2013 17:21 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 24-11-2013 17:58, richard wrote:
> In BASIC I would run a for loop to print out the corresponding characters
> for a given value.
>
> for x=65 to 90
> print chr$(x)
> next x
>
> would give ABCDEF......
>
> how is this done in php?
>

http://us1.php.net/manual/en/function.chr.php
Re: converting numbers to ascii values [message #183892 is a reply to message #183890] Sun, 24 November 2013 17:42 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 24/11/13 17:06, Lew Pitcher wrote:
> On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
> <noreply(at)example(dot)com> wrote:
>
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>
> for ($x = 65; $x <=90; ++$x)
> printf("%c",$x);
>
>
> HTH
>
for ($x = 65; $x <91; $x++)
echo($x);

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: converting numbers to ascii values [message #183895 is a reply to message #183891] Sun, 24 November 2013 18:15 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:

> On 24-11-2013 17:58, richard wrote:
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>>
>
> http://us1.php.net/manual/en/function.chr.php

thanks that was what I was looking for.
In my array the key is one letter.
"A"=>array()
"B"=>array()
"C"=>array()

I need to step through each character to get the list.
What is a good way to do that?

BASIC style

x=65
while x<91
a$=chr$(x)
print a$
x=x+1
wend
Re: converting numbers to ascii values [message #183896 is a reply to message #183892] Sun, 24 November 2013 18:39 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Sunday 24 November 2013 12:42, in comp.lang.php, "The Natural
Philosopher" <tnp(at)invalid(dot)invalid> wrote:

> On 24/11/13 17:06, Lew Pitcher wrote:
>> On Sunday 24 November 2013 11:58, in comp.lang.php, "richard"
>> <noreply(at)example(dot)com> wrote:
>>
>>> In BASIC I would run a for loop to print out the corresponding
>>> characters for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>>
>> for ($x = 65; $x <=90; ++$x)
>> printf("%c",$x);
>>
>>
>> HTH
>>
> for ($x = 65; $x <91; $x++)
> echo($x);
>

Nope. That just gets the numeric values of $x, not the ASCII (or other
characterset) equivalent.

~ $ echo '<?php for ($x = 65; $x < 91; $x++) echo($x); echo PHP_EOL ?>' |
php
6566676869707172737475767778798081828384858687888990
~ $

The alternative I suggested gives

~ $ echo '<?php for ($x = 65; $x <= 90; ++$x) printf("%c",$x);
echo PHP_EOL ?>' | php
ABCDEFGHIJKLMNOPQRSTUVWXYZ
~ $


HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: converting numbers to ascii values [message #183897 is a reply to message #183895] Sun, 24 November 2013 20:53 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 24/11/13 19:15, richard wrote:
> On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:
>
>> On 24-11-2013 17:58, richard wrote:
>>> In BASIC I would run a for loop to print out the corresponding characters
>>> for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>>>
>>
>> http://us1.php.net/manual/en/function.chr.php
>
> thanks that was what I was looking for.
> In my array the key is one letter.
> "A"=>array()
> "B"=>array()
> "C"=>array()
>
> I need to step through each character to get the list.
> What is a good way to do that?
>
> BASIC style
>
> x=65
> while x<91
> a$=chr$(x)
> print a$
> x=x+1
> wend
>
>

for-loop works fine, you can also use foreach-loop if you have a list of
keys stored in a variable.

--

//Aho
Re: converting numbers to ascii values [message #183898 is a reply to message #183890] Sun, 24 November 2013 21:46 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Lew Pitcher wrote:

> "richard" wrote:
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>
> for ($x = 65; $x <=90; ++$x)
> printf("%c",$x);

There is a probably more efficient alternative to this shell-script/C like
way in PHP:

for ($i = 65; $i <= 90; ++$i)
{
echo chr($i);
}

And finally:

$ php -r 'var_dump(implode("", array_map("chr", array_keys(array_fill(65,
26, true)))));'
string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


PointedEars
--
> If you get a bunch of authors […] that state the same "best practices"
> in any programming language, then you can bet who is wrong or right...
Not with javascript. Nonsense propagates like wildfire in this field.
-- Richard Cornford, comp.lang.javascript, 2011-11-14
Re: converting numbers to ascii values [message #183899 is a reply to message #183898] Sun, 24 November 2013 22:01 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Thomas 'PointedEars' Lahn wrote:

> Lew Pitcher wrote:
>> "richard" wrote:
>>> In BASIC I would run a for loop to print out the corresponding
>>> characters for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......
>>>
>>> how is this done in php?
>
> […]
> $ php -r 'var_dump(implode("", array_map("chr", array_keys(array_fill(65,
> 26, true)))));'
> string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

That was a quick hack; this is better:

$ php -r 'var_dump(implode("", array_map("chr", range(65, 90))));'
string(26) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Note how PHP's range() differs from, e.g., Python's: <http://php.net/range>


PointedEars :)
--
When all you know is jQuery, every problem looks $(olvable).
Re: converting numbers to ascii values [message #183902 is a reply to message #183889] Mon, 25 November 2013 02:47 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
richard <noreply(at)example(dot)com> wrote in news:1os8m6ou5g6i4$.1n33ilnevl0le.dlg@
40tude.net:

> In BASIC I would run a for loop to print out the corresponding characters
> for a given value.
>
> for x=65 to 90
> print chr$(x)
> next x
>
> would give ABCDEF......
>
> how is this done in php?
>
http://lmgtfy.com/?q=convert+number+to+ascii+value+using+php
Re: converting numbers to ascii values [message #183903 is a reply to message #183897] Mon, 25 November 2013 02:50 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
"J.O. Aho" <user(at)example(dot)net> wrote in news:bff7adFecqpU1(at)mid(dot)individual(dot)net:

>
> for-loop works fine, you can also use foreach-loop if you have a list of
> keys stored in a variable.
>
You don't understand. RtS isn't asking for someone to tell him how to write it -- he's asking for
someone to write it for him.
Re: converting numbers to ascii values [message #183904 is a reply to message #183902] Mon, 25 November 2013 03:30 Go to previous messageGo to next message
Mr Oldies is currently offline  Mr Oldies
Messages: 241
Registered: October 2013
Karma: 0
Senior Member
On Mon, 25 Nov 2013 02:47:59 +0000 (UTC), Doug Miller wrote:

> richard <noreply(at)example(dot)com> wrote in news:1os8m6ou5g6i4$.1n33ilnevl0le.dlg@
> 40tude.net:
>
>> In BASIC I would run a for loop to print out the corresponding characters
>> for a given value.
>>
>> for x=65 to 90
>> print chr$(x)
>> next x
>>
>> would give ABCDEF......
>>
>> how is this done in php?
>>
> http://lmgtfy.com/?q=convert+number+to+ascii+value+using+php

http://lmgtfy.com/?q=shove+it+up+your+ass
Re: converting numbers to ascii values [message #183905 is a reply to message #183903] Mon, 25 November 2013 03:36 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 11/24/2013 9:50 PM, Doug Miller wrote:
> "J.O. Aho" <user(at)example(dot)net> wrote in news:bff7adFecqpU1(at)mid(dot)individual(dot)net:
>
>>
>> for-loop works fine, you can also use foreach-loop if you have a list of
>> keys stored in a variable.
>>
> You don't understand. RtS isn't asking for someone to tell him how to write it -- he's asking for
> someone to write it for him.
>

Doug,

J.O. understands Richard quite well. But for others reading the
newsgroup, J.O.'s update can be helpful.

And I see Richard has his usual (lack of) intelligent response to you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: converting numbers to ascii values [message #183906 is a reply to message #183889] Mon, 25 November 2013 03:40 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 24 Nov 2013 11:58:34 -0500, richard wrote:

> In BASIC I would run a for loop to print out the corresponding
> characters for a given value.
>
> for x=65 to 90 print chr$(x)
> next x
>
> would give ABCDEF......
>
> how is this done in php?

Using a combination of the following:

http://php.net/manual/en/control-structures.for.php
http://us3.php.net/chr
http://us3.php.net/print

Or in other words, just about the same way it is in basic, but using the
equivalent php control structures and functions instead.

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
Re: converting numbers to ascii values [message #183908 is a reply to message #183905] Mon, 25 November 2013 05:47 Go to previous messageGo to next message
J.O. Aho is currently offline  J.O. Aho
Messages: 194
Registered: September 2010
Karma: 0
Senior Member
On 25/11/13 04:36, Jerry Stuckle wrote:
> On 11/24/2013 9:50 PM, Doug Miller wrote:
>> "J.O. Aho" <user(at)example(dot)net> wrote in
>> news:bff7adFecqpU1(at)mid(dot)individual(dot)net:
>>
>>>
>>> for-loop works fine, you can also use foreach-loop if you have a list of
>>> keys stored in a variable.
>>>
>> You don't understand. RtS isn't asking for someone to tell him how to
>> write it -- he's asking for
>> someone to write it for him.
>>
>
> Doug,
>
> J.O. understands Richard quite well. But for others reading the
> newsgroup, J.O.'s update can be helpful.

Thanks for replying for me, you are spot on as usually.


> And I see Richard has his usual (lack of) intelligent response to you.

It's his problem.

--

//Aho
Re: converting numbers to ascii values [message #183910 is a reply to message #183904] Mon, 25 November 2013 06:12 Go to previous messageGo to next message
Evan Platt is currently offline  Evan Platt
Messages: 124
Registered: November 2010
Karma: 0
Senior Member
On Sun, 24 Nov 2013 22:30:38 -0500, richard <noreply(at)example(dot)com>
wrote:

> http://lmgtfy.com/?q=shove+it+up+your+ass

And you wonder why people only help you once...
--
To reply via e-mail, remove The Obvious and .invalid from my e-mail address.
Re: converting numbers to ascii values [message #183915 is a reply to message #183895] Mon, 25 November 2013 12:16 Go to previous message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 24-11-2013 19:15, richard wrote:
> On Sun, 24 Nov 2013 18:21:43 +0100, Luuk wrote:
>
>> On 24-11-2013 17:58, richard wrote:
>>> In BASIC I would run a for loop to print out the corresponding characters
>>> for a given value.
>>>
>>> for x=65 to 90
>>> print chr$(x)
>>> next x
>>>
>>> would give ABCDEF......

no, it gives:
A
B
C
D
......

>>>
>>> how is this done in php?
> What is a good way to do that?
>
> BASIC style
>
> x=65
> while x<91
> a$=chr$(x)
> print a$
> x=x+1
> wend
>
>

this also gives:
A
B
C
D
......

What about trying to copy this to PHP:

$x=65;
while ($x<91) {
a$=chr($x);
print $a;
$x=$x+1;
}



RTFM:
http://us2.php.net/while

o, BTW, i left (at least) 1 error in above code,
maybe you can find it ???
;)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PHP functions to convert markup efficiently
Next Topic: creating key / hash
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 10:19:47 GMT 2024

Total time taken to generate the page: 0.02232 seconds