简体   繁体   English

Java字节数组到Ruby

[英]Java byte array to Ruby

I have a Desktop application using java swing frames.我有一个使用 java 摆动框架的桌面应用程序。 I have to rewrite the application to either Ruby or python.我必须将应用程序重写为 Ruby 或 python。 However I understand java to certain extent - I need help rewriting certain piece of the code in java.但是我在一定程度上了解 java - 我需要帮助重写 java 中的某些代码。

1. 1.

byte[] ModuleGuid = new byte[]{
        (byte)0xe1, (byte)0x9a, (byte)0x69, (byte)0x01,
        (byte)0xb8, (byte)0xc2, (byte)0x49, (byte)0x80,
        (byte)0x87, (byte)0x7e, (byte)0x11, (byte)0xd4,
        (byte)0xd8, (byte)0xf1, (byte)0xbe, (byte)0x79
};

2. 2.

JAVA_PvAPI_SensorInfoEx[] lptSensorInfo =
        new JAVA_PvAPI_SensorInfoEx[(int)PsConstant.JAVA_PvAPI_GET_SENSOR_INFO_MAX];
  1. There's no such thing as byte in Ruby. Ruby 中没有byte这样的东西。 When you want to store a sequence of bytes, you store it in a string:当你想存储一个字节序列时,你将它存储在一个字符串中:

     module_guid = "\\xE1\\x9A\\x69\\x01...".force_encoding('ASCII-8BIT')

    If you want to copy and paste from Java code, then use:如果要从 Java 代码复制和粘贴,请使用:

     module_guid = [0xe1, 0x9a, ...].pack('c*')

    By the way, I didn't declare the variable as ModuleGuid because capitalized identifiers are for constants in Ruby, and I didn't see the final keyword in your Java code.顺便说一下,我没有将变量声明为ModuleGuid因为大写标识符是用于 Ruby 中的常量,而且我在您的 Java 代码中没有看到final关键字。

  2. Arrays in Ruby are just arrays. Ruby 中的数组只是数组。 They can store any kind of objects.它们可以存储任何类型的对象。 There's no int[] or String[] or Whatever[] in Ruby. Ruby 中没有int[]String[]Whatever[] Ruby arrays don't have fixed sizes and can be expanded or shrink any time. Ruby 数组没有固定大小,可以随时扩展或收缩。 Your second piece of Java code can be rewritten as:您的第二段 Java 代码可以重写为:

     sensor_info = []

    But, depending on your use-case, this statement itself may not be necessary because in Ruby you have sooooo many ways to obtain an array.但是,根据您的用例,此语句本身可能不是必需的,因为在 Ruby 中您有太多获取数组的方法。

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

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