Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string to ASCII value in PHP without ord()?

Tags:

php

ascii

I am looking to convert a string say 'Hello' world to its ASCII value in php. But I don't want to use ord(). Are there any other solutions for printing ascii value without using ord()?

like image 213
Rani Avatar asked Feb 22 '17 17:02

Rani


1 Answers

unpack()

Unpacks from a binary string into an array according to the given format.

Use the format C* to return everything as what you'd get from ord().

print_r(unpack("C*", "Hello world"));
Array
(
    [1] => 72
    [2] => 101
    [3] => 108
    [4] => 108
    [5] => 111
    [6] => 32
    [7] => 119
    [8] => 111
    [9] => 114
    [10] => 108
    [11] => 100
)
like image 104
user3942918 Avatar answered Sep 19 '22 20:09

user3942918