I am attempting to convert the following Java code to PHP but I'm having issues with finding the equivalent of getBytes()
JAVA CODE
import java.math.BigInteger;
public class Test {
public static void main(String[] ar){
String a="4F23DE12";
BigInteger b = new BigInteger(a.getBytes());
System.out.println(b); //Result is 3766753334112104754
}
}
The Java result is 3766753334112104754 but my attempt with PHP is not giving the same result.
PHP CODE
use phpseclib3\Math\BigInteger;
$a="4F23DE12";
$b = new BigInteger($a);
echo $b; //Result is 4
The PHP result is 4. The issue seems to be from getBytes() and I have tried String to byte array in php and php equivalent of java getBytes() but still no solution.
In debugging it, I also tried System.out.println("4F23DE12".getBytes()); in Java, the result was [B#6d06d69c but I haven't found an exact equivalent in PHP.
Please I will appreciate any help on how to use PHP to get the same Java result (3766753334112104754)
My question was solved by #ErwinBolwidt. Check the comments for more info.
With #ErwinBolwidt solution, the PHP equivalent of "4F23DE12".getBytes() is new BigInteger("4F23DE12", 256).
Both will give you 3766753334112104754
Related
This seems to have been asked in several places and has been marked as "closed" and "off-topic". However, people seem to have this problem constantly
invoking a php method from java (closed)
Calling PHP from Java (closed)
How can I run PHP code within a Java application? (closed)
This answer in the last question partly answers this but does not clarify how to read the outputs.
I finally found the answer to the question:
How do I run a PHP program from within Java and obtain its output?
To give more context, someone has given me a PHP file containing code for some method foo that returns a string. How do we invoke this from JVM?
Searching on Google is not helpful as all articles I found don't explain how to call PHP from Java but rather Java from PHP.
The answer below explains how to do this using the PHP/Java bridge.
The answer is in Scala but would be easy to read for Java programmers.
Code created from this SO answer and this example:
package javaphp
import javax.script.ScriptEngineManager
import php.java.bridge._
import php.java.script._
import php.java.servlet._
object JVM{ // shared object for PHP/JVM communication
var out = ""
def put(s:String) = {
out = s
}
}
object Test extends App {
val engine = (new ScriptEngineManager).getEngineByExtension("php")
val oldCode = """
<?php
function foo() {
return 'hello';
// some code that returns string
}
?>
"""
val newCode = """
<?php
$ans = foo();
java('javaphp.JVM')->put($ans);
?>
"""+oldCode
// below evaluates and returns
JVM.out = "" //reset shared output
engine.eval(newCode)
println("output is : "+JVM.out) // prints hello
}
To run this file:
Install PHP, Scala and set path correctly. Then create a file php.scala with the above code. Then run:
scalac php.scala
and
scala javaphp.Test
I have an web application using google translate API. It developed in java using google-api-services-translate.
It has some problem translate Japanese to another language. Actually write in Japanese pronunciation like a "Sayonara".
I tried translate "Sayonara" on translate.google.com. Its result is "farewell".
but my application's result is "Sayonara"
For Example (using cUrl),
$ curl -X POST -H"Content-Type:application/json" -d"{\"texts\":[\"sayonara\"],\"target\":\"en\"}" http://127.0.0.1/translate
result is,
[{"detectedSourceLanguage":"ja","translatedText":"sayonara"}]
Another example,
$ curl -X POST -H"Content-Type:application/json" -d"{\"texts\":[\"ăăăȘă\"],\"target\":\"en\"}" http://127.0.0.1/translate
result is,
[{"detectedSourceLanguage":"ja","translatedText":"Farewell"}]
How can I get a result "Farewell"?
using java source is,
public List list(java.util.List<java.lang.String> q, java.lang.String target) throws java.io.IOException {
List result = new List(q, target);
initialize(result);
return result;
}
//----------------------------------------------------
Translate translate = builder.build();
Translate.Translations.List list = translate.translations().list(req.texts, req.target);
I have a Java program which has a static method
private static int checkURL(String currentURL)
From my perl script, I want to call this method and get return value of this method.
I have a constraint that I can't use any inbuilt Perl modules offered by CPAN.
I am thinking to call Java through system() command in Perl but issue is how to call this method and get the return code?
call external command and get back result (straight from http://www.perlhowto.com/executing_external_commands)
#-- scalar context
$result = `command arg1 arg2`;
Using Java module
use Java;
my $java = new Java;
my $obj = $java->create_object("com.my.Class","constructor parameter");
$obj->checkURL("http://www.google.com/");
$obj->setId(5);
Iniline::Java is also a famous module for Java/Perl integration.
Edit: I have a constraint that I can't use any inbuilt perl modules offered by CPAN.
Oh sorry I didn't see that. I had tried something like below some time ago.
Hello.java
class Hello {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program to test return code.\n");
System.exit(100);
}
}
test.pl
#!/usr/bin/perl
use strict;
use warnings;
my $command = "java Hello";
print "Command is $command:\n";
system($command);
my $retval = $? >> 8;
print "The return code is $?\n";
print "retval is $retval\n";
Run it
perl test.pl
Output
Command is java Hello:
This is a simple Java program to test return code.
The return code is 25600
retval is 100
I want to pass String containing contents of xml file to native function using JNA .But somehow it is giving me problems. The program goes into infinite loop and does not get terminated. The same thing is working when I am trying to access DLL through C.
This is how my code looks like -
Native side --
Class ABC{
...
long t = processValues(const * str1 ,char** output);
...}
JNA interface looks llke this -
public interface Add extends Library
{
Add INSTANCE = (Add) Native.loadLibrary("add", Add.class);
...
NativeLong processValues(String str1,PointerByReference output);
...}
main method in java class is as follows -
public static void main(String args[]){
Add lib = Add.INSTANCE;
PointerByReference ptrRef = new PointerByReference();
String strBuffer = "<?xml version= \"1.0\" ?><NRECORD> <SUBRECORD><ITEM1> <NAME> pqr</NAME> <MDATE>10/12/2012</MDATE><ENGINEER>TMAY</ENGINEER></ITEM1></SUBRECORD></NRECORD> "
Nativelong p = lib.processValues(strBuffer,ptrRef);
}
The program goes into infinite loop and never get terminated. DLL uses recursive function to parse input xml string, I think this is where problem lies. (I am using third party dll so cant access code.) But function processValues() get executed successfully when same dll is accessed through C.(with same input parameters) My questions are
is this right way to pass xml contents as string?
Is there any way by which I can get event logs how the dll functions are getting called.
Thanks in advance.
Although I've been using Scala for a while and have mixed it with Java before, I bumped on a problem.
How can I pass a Java array to Scala? I know that the other way around is fairly straightforward. Java to Scala is not so however.
Should I declare my method in Scala?
Here is a small example of what I'm trying to achieve:
Scala:
def sumArray(ar: Array[Int]) = ...
Java:
RandomScalaClassName.sumArray(new int[]{1,2,3});
Is this possible?
absolutely!
The Array[T] class in Scala is mapped directly to the Java type T[]. They both have exactly the same representation in bytecode.
At least, this is the case in 2.8. Things were a little different in 2.7, with lots of array boxing involved, but ideally you should be working on 2.8 nowadays.
So yes, it'll work exactly as you've written it.
Yes, it is totally possible and in fact very easy. The following code will work as expected.
// TestArray.scala
object TestArray {
def test (array: Array[Int]) = array.foreach (println _)
}
-
// Sample.java
public class Sample
{
public static void main (String [] args) {
int [] x = {1, 2, 3, 4, 5, 6, 7};
TestArray.test (x);
}
}
Use the following command to compile/run.
$scalac TestArray.scala
$javac -cp .:/opt/scala-2.8.0/lib/scala-library.jar Sample.java
$java -cp .:/opt/scala-2.8.0/lib/scala-library.jar Sample