tags:

views:

7614

answers:

6

How can I get the byte array from some string witch can contains numbers,letters and so on!? If u are familiar with java I am looking for same functionality of getBytes() method.

I tried with snippet like this one:

for($i = 0; $i < strlen($msg); $i++){
    $data.=ord($msg[$i]);
        //or $data[]=ord($msg[$1]); 
}

but without success I think, so any kind of help will be apreciated.

Ps. Why do I need this at all!? Well I need to send via fputs() bytearray to server written in java...

+1  A: 

You could try this:

$in_str = 'this is a test';
$hex_ary = array();
foreach (str_split($in_str) as $chr) {
    $hex_ary[] = sprintf("%02X", ord($chr));
}
echo implode(' ',$hex_ary);
karim79
+2  A: 
print_r(unpack("H*","The quick fox jumped over the lazy brown dog"))

Array ( [1] => 54686520717569636b20666f78206a756d706564206f76657220746865206c617a792062726f776e20646f67 )

T = 0x54, h = 0x68, ...

You can split the result into two-hex-character chunks if necessary.

Sparr
This isn't a solution for a problem!
Splendid
A: 

Is this solved yet? I have ran to the same problem? These solutions above don't work..

Thanks for your reply

Ok i'll try to be more spesific with my problem..

I'm trying to call java webservice which does the password encoding like this:

    String password = "Password";
    try{
    MessageDigest md5 = MessageDigest.getInstance("MD5");

    byte[] key = md5.digest( (password).getBytes());
    String encodedKey = new sun.misc.BASE64Encoder().encode(key);

    System.out.println(encodedKey);
    }catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

Java result is: 3GR+tl5nEeFVN1IYISs5ZA==

And the php side:

$password = "Password";

$key = md5($password,true); $encodedKey = base64_encode($key);

echo $key;

The result with PHP is: Üd~¶^gáU7R!+9d And when the md5 true parameter is false: dc647eb65e6711e155375218212b3964

So do you see my problem?

Ok I think I solved it :) I had one php variable in wrong place :)

post a new question or comment instead. writing a question in the answer section just will confuse other users!
thephpdeveloper
A: 

In PHP, strings are bytestreams. What exactly are you trying to do?

Re: edit

Ps. Why do I need this at all!? Well I need to send via fputs() bytearray to server written in java...

fputs takes a string as argument. Most likely, you just need to pass your string to it. On the Java side of things, you should decode the data in whatever encoding, you're using in php (the default is iso-8859-1).

troelskn
A: 

Hi, could you please describe what was exactly your solution? We are having a similar situation here, although with SHA1 instead of MD5, so chances are we could probably learn from your experience. Thanks in advance!

We changed the sever-client communication protocol and we avoided stupid problems like this one :D
Splendid
A: 

I had the need to authenticate my PHP scripts from a table administered from a Java Application made by some partners at work. Finally after a couple of hours of research I came to this snippet that could work well as an example. The field bytepassword is a LOB, which I didn't know how to use until this. You have to have this a transaction, notice the "begin and "commit" queries at the beginning and the end of the code. The last part just verifies the two MD5 digests, the one found on the database and some data you would gather from a login page. Notice the flag on the md5 instruction, it will output the string in raw binary value, just as it is retrieved from the database. Hope it saves time to someone in the same situation:

<?php
    //This snippet supposes you're already connected to a PG database
    pg_query($dbp, "begin");
    $query = "SELECT bytepassword
    FROM my_table
    WHERE id=1";
    $result = pg_query($dbp, $query) or die("Error executing query $query");
    $fila = pg_num_rows($result);
    $row = pg_fetch_array($result);
    $oid = $row[0];
    echo $oid."\n";
    $lob = pg_lo_open($dbp,$oid,"r");
    $datos = pg_lo_read($lob);
    echo $datos."\n";
    pg_query($dbp, "commit");
    echo $datos2 = md5("my_password",true);
    echo ($datos === $datos2)?"1":"0";
?>

Cheers,

coolmig