tags:

views:

72

answers:

2
public static String convertHexString(String hex){
 byte[] bytes = new byte[hex.length() / 2];
 for (int i = 0; i < bytes.length; i++) {
  bytes[i] = (byte) Integer.parseInt(hex.substring(2 * i,2 * i + 2), 16);
 }  
 sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
 System.out.println(encoder.encode(bytes));
 return encoder.encode(bytes);
}

above is the function written in java, i want to convert it into the php function.

This function accepts the HEX string as a parameter.

basically i want to convert a string from hex to string.

the string should be binary.

Please can anybody help me.

Thanks in advance :)

+3  A: 

It's just a matter of doing:

base64_encode(pack("H*", $string));

pack with the H* argument converts an arbitrary-length string with hexadecimal digits (each group of two digits representing a byte and the first one more significant) into a binary string. See also base64_encode.

Artefacto
hey thanks for your reply.I have tried pack() and base64_encoding. It works but still gives me the error.Let me brief you what m upto.I Am sending this converted string to an api using CURL.The parameters i am sending are encoded with json_encode().{"error":{"message":"Failed to unmarshal input:[Lcom.bea.wlcp.wlng.schema.ews.binary_sms.BinaryMessage;","type":"com.bea.wlcp.wlng.rest.bind.UnmarshalException"}}ie. its not invoking the remote method.Please help me with this.
dirtycode