我有一个用Java编写的类型代码:
public static String asHex(byte[] buf)
{
StringBuilder strbuf = new StringBuilder(buf.length << 1);
for (byte aByte : buf) {
if (((int) aByte & 0xff) < 0x10) {
strbuf.append('0');
}
strbuf.append(Long.toString((int) aByte & 0xff, 16));
}
return strbuf.toString();
}这和PHP中的base64_decode是一样的吗?
发布于 2011-05-24 07:55:28
如果您有时间,最简单的方法是在您的函数和PHP函数中运行相同的字符串,并查看它们是否产生相同的结果。或者,如果你真的有空闲的时间,读一读this,看看你是否能解决这个问题。
编辑:在进一步研究之后,它似乎只是将字节数组转换为十六进制字符串,正如您所指出的,这不是base64编码。
我认为PHP函数相当于它所做的事情是bin2hex。
发布于 2011-05-25 20:12:22
正如@James所说: bin2hex会这样做:
<?php
$str = "Hello world!";
echo bin2hex($str) . "<br />";
?>所产生的结果与
static public void main(String args[]) {
String str= "Hello world!";
byte[] x = str.getBytes();
String s = asHex(x);
System.out.println (s);
}
public static String asHex (byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}https://stackoverflow.com/questions/6104316
复制相似问题