简体   繁体   English

将actionscript float转换为java

[英]Convert actionscript float to java

I need to convert this actionscript code to java: 我需要将此actionscript代码转换为java:

p = 0xe8af7c;    
var ba: ByteArray = new ByteArray();
ba.writeFloat ((p >> 16 & 0xFF) / 255);
ba.writeFloat ((p >> 8 & 0xFF) / 255);
ba.writeFloat ((p & 0xFF) / 255);

This is what I have come up with: 这就是我想出的:

int p = 0xe8af7c;
DataOutputStream dos = new DataOutputStream(new FileOutputStream(output));
dos.writeFloat ((p >> 16 & 0xFF) / 255f);
dos.writeFloat ((pix >> 8 & 0xFF) / 255f);
dos.writeFloat ((pix & 0xFF) / 255f);

But this does not produce the same results. 但这不会产生相同的结果。 It should produce 3f 68 e8 e9 3f 2f af b0 3e f8 f8 f9 , but it returns 3f 68 d7 98 d7 99 3f 2f c2 af c2 b0 3e d7 a8 d7 a8 d7 a9 . 它应该产生3f 68 e8 e9 3f 2f af b0 3e f8 f8 f9 ,但它返回3f 68 d7 98 d7 99 3f 2f c2 af c2 b0 3e d7 a8 d7 a8 d7 a9 Does it mean actionscript handles floats different than java and this function cannot be converted like this? 这是否意味着actionscript处理不同于java的浮动,这个函数不能像这样转换? How should I convert this code? 我该如何转换此代码?

EDIT: The purpose of this is to get the three digit pairs of p - e8, af, and 7c, convert them to integers, which are 232, 175, 124, then divide those values by 255, and write to DataOutputStream them. 编辑:这样做的目的是得到三个数字对的p - e8,af和7c,将它们转换为整数,即232,175,124,然后将这些值除以255,并写入DataOutputStream它们。

Using this code 使用此代码

import org.apache.commons.codec.binary.Hex;

public static void main(String[] args) throws Exception {
    int p = 0xe8af7c;
    ByteArrayOutputStream bos = new ByteArrayOutputStream(12);
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeFloat((p >> 16 & 0xFF) / 255f);
    dos.writeFloat((p >> 8 & 0xFF) / 255f);
    dos.writeFloat((p & 0xFF) / 255f);
    System.out.println(Hex.encodeHexString(bos.toByteArray()));
}

I get the following output: 我得到以下输出:

3f68e8e93f2fafb03ef8f8f9

which matches your desired result 符合您想要的结果

3f 68 e8 e9 3f 2f af b0 3e f8 f8 f9

So you're probably writing something extra to your output that you aren't showing us here. 所以你可能正在为你的output写一些额外的东西,你没有在这里向我们展示。

Also take a look at ByteBuffer for an fancy byte array with conversion methods. 另请ByteBuffer了解带有转换方法的花式字节数组。

You are writing data correctly into DataOutputStream , but it gets damaged when you pipe it into output because the data has to go to console, and your bytes are interpreted as ANSI chars and get "corrected" into UTF-8 charset, resulting in two bytes being sent to the console, which you in turn capture via OS pipeline into a file. 您正在将数据正确地写入DataOutputStream ,但是当它将其传输到output时它会被损坏,因为数据必须转到控制台,并且您的字节被解释为ANSI字符并被“纠正”为UTF-8字符集,从而产生两个字节被发送到控制台,然后通过操作系统管道将其捕获到文件中。 To fix, use a FileOutputStream with a normal file as destination instead of output . 要修复,请使用FileOutputStream将普通文件作为目标而不是output

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

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