Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:50:48+00:00 2026-05-15T23:50:48+00:00

hey all i am trying to make a data output stream in php to

  • 0

hey all i am trying to make a data output stream in php
to write back primitive data types to a java application

i created a class that write the data to an array
(write it same as java do , copy from java code)

and finally i am writing back the array to the client.

feels like its not working well

for example the writeInt method
send to the java client some wrong values
am i doing ok ?

thank you

here is my code

private $buf   = array();


public function writeByte($b) {
  $this->buf[] = pack('c' ,$b);
}

public function writeInt($v) { 
  $this->writeByte($this->shiftRight3($v , 24) & 0xFF);
  $this->writeByte($this->shiftRight3($v , 16) & 0xFF);
  $this->writeByte($this->shiftRight3($v ,  8) & 0xFF);
  $this->writeByte($this->shiftRight3($v ,  0) & 0xFF);

}


private function shiftRight3($a ,$b){
  if(is_numeric($a) && $a < 0){
    return ($a >> $b) + (2<<~$b);
  }else{
    return ($a >> $b);
  }
}


public function toByteArray(){
    return $this->buf;
}

this is how i am setting the main php file

   header("Content-type: application/octet-stream" ,true);
   header("Content-Transfer-Encoding: binary" ,true);

this is how i am returning the data

  $arrResult = $dataOutputStream->toByteArray();
  for ($i = 0 ; $i < count($arrResult) ; $i ++){
     echo $arrResult[$i];
  }

I EDIT THE QUESTION ,ACCOURDING TO MY CODE CHANGING
in the java client side seems that i have 2 bytes to read start always
i have 13 , 10 , which is \r \n
how come i am reading them always ?

(in my test i am sending one byte to the java client side ,

  URL u = new URL("http://localhost/jtpc/test/inputTest.php");
  URLConnection c = u.openConnection();

  InputStream in =  c.getInputStream();
  int read = 0;
  for (int j = 0; read != -1 ; j++) {
    read = in.read();
    System.out.println("More to read : " + read);
  }
 )

  the output will be ,
   More to read : 13
   More to read : 10
   More to read : 1 (this is the byte i am sending)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:50:49+00:00Added an answer on May 15, 2026 at 11:50 pm

    Php has pack() function for turning data into binary form. Unpack() reverses the operation.

    $binaryInt = pack('I', $v);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey all i am trying to find examples of getting data back from a
Hey all I'm trying to make a 3d game in with 2d sprite characters.
Hey all, my first post :D Problem: I'm trying to make a template gallery,
hey all, i been trying to make a class that uses a dictionary to
Hey all, I'm am trying to make a very interactive UI with lots of
Hey all. I'm trying to see about handling events in a console application. I
Hey all, I am setting up a PHP web app that will make use
Hey all, so I'm just trying to make a basic chatbox on my site
hey all, i have a question about urlmapping in grails. I'm trying to make
Hey all i am trying to figiure out why my code is not triggering

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.