简体   繁体   English

在PHP中将字符串转换为utf-8

[英]Convert string into utf-8 in php

There is a script written in Java and I am trying to convert it into PHP, but I'm not getting the same output. 有一个用Java编写的脚本,我正在尝试将其转换为PHP,但是没有得到相同的输出。

How can I get it in PHP same as in Java? 如何在PHP中获得与Java中相同的效果?

Script written in Java 用Java编写的脚本

String key = "hghBGJH/gjhgRGB+rfr4654FeVw12y86GHJGHbnhgnh+J+15F56H";

byte[] kSecret = ("AWS4" + key).getBytes("UTF8");

Output: [B@167cf4d 输出: [B@167cf4d

Script written in PHP 用PHP编写的脚本

$secret_key = "hghBGJH/gjhgRGB+rfr4654FeVw12y86GHJGHbnhgnh+J+15F56H";
utf8_encode("AWS4".$secret_key);

Output: AWS4hghBGJH/gjhgRGB+rfr4654FeVw12y86GHJGHbnhgnh+J+15F56H 输出: AWS4hghBGJH/gjhgRGB+rfr4654FeVw12y86GHJGHbnhgnh+J+15F56H

The result [B@167cf4d you are getting is a toString() call on the byte array. 您得到的结果[B@167cf4d是对字节数组的toString()调用。 The [B means the value is a byte array. [B表示该值是一个字节数组。 @167cf4d is it's location within the virtual memory. @167cf4d是它在虚拟内存中的位置。 You simply cannot get the PHP script to give you the same result. 您根本无法获得PHP脚本来提供相同的结果。 What you need to do is fix the printing on the Java side, but we don't know what API you're using to print it and where. 您需要做的是在Java端修复打印,但是我们不知道您使用什么API以及在哪里打印。 Is it a web-application, a local application, etc... ? 它是Web应用程序,本地应用程序等吗?

edit: 编辑:
Since you're using it in a Java web-application, there are two likely scenarios. 由于您在Java Web应用程序中使用它,因此有两种可能的情况。 The code is either in a Servlet, or in a JSP. 该代码位于Servlet或JSP中。 If it's in a servlet, you have to set your output to UTF-8, then you can simply print the string like so: 如果它在servlet中,则必须将输出设置为UTF-8,然后可以像这样简单地打印字符串:

        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.write("AWS4");
        out.write(key);

If it's in a JSP, that makes it a bit more inconvenient, because you have to make sure you don't leave white-spaces like new lines before the printing begins. 如果是在JSP中,这会带来一些不便,因为在打印开始之前,必须确保不要像换行那样保留空白。 It's not a problem for regular HTML pages, but it's a problem if you need precisely formatted output to the last byte. 对于常规的HTML页面来说,这不是问题,但是如果您需要精确格式化输出到最后一个字节,这是一个问题。 The response and out objects exist readily there. responseout对象很容易在那里存在。 You will likely have some <%@page tags with attributes there, so you have to make sure you're opening the next one as you're closing the last. 您可能会有一些带有属性的<%@page标签,因此您必须确保在关闭最后一个标签时打开下一个标签。 Also a new line on the end of file could skew the result, so in general, JSP is not recommended for white-space-sensitive data output. 另外,文件末尾的新行可能会使结果倾斜,因此通常不建议将JSP用于对空格敏感的数据输出。

You can't sysout an byte array without converting it to a string, UTF-8 by default, toString() is being called,when you do something like: 您不能在不将字节数组转换为字符串的情况下对字节数组进行sysout操作,默认情况下,执行以下操作时会默认调用UTF-8 toString():

System.out.println("Weird output: " + byteArray);

This will output the garbage you mentioned above. 这将输出您上面提到的垃圾。 Instead, create a new instance of the standard String class. 而是,创建标准String类的新实例。

System.out.println("UTF-8: " + new String(byteArray));

In Java you get the object reference of the byte array, not its value. 在Java中,您获得字节数组的对象引用,而不是其值。 Try: 尝试:

new String(kSecret, "UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM