CPP 到 Java 的转换
这是我的场景。我有一个用 C++ 编写的应用程序,但不是完整的源代码,但它的“内容”就在那里。我还有这个应用程序的已编译 exe。它与我们网络上某处的服务器进行通信。我试图在java中复制C++代码,但是它使用双字和内存引用、sizeof等,所有java中不存在的东西,因为它管理自己的内存。它构建这个大型复杂消息,然后通过网络发送它。因此,我基本上是嗅探流量并检查数据包,并尝试对其发送的数据进行硬编码,以查看是否可以通过这种方式从服务器获得响应。但是我似乎无法完美地复制该消息。其中一些,例如它发送的许可证代码,是“明文十六进制”,即转换为 ascii 的十六进制,而数据的其他一些部分不是“明文十六进制”,例如“aa”,它不翻译成ascii(或者至少是一个通用字符集??如果这有任何意义,我不确定)。
理想情况下,我不想这样做,但这是看看是否可以让服务器响应我的垫脚石。其中一项功能是让应用程序自行注册,这就是我试图复制的功能。
我上面的一些假设可能是错误的,所以我提前道歉。感谢您抽出时间。
Here's my scenario. I have an application written in C++ but not the complete source but the "meat" of it is there. I also have a compiled exe of this application. It communicates to a server somewhere here on our network. I am trying to replicate the C++ code in java, however it uses dwords and memory references, sizeof etc, all things that don't exist in java since it manages it's own memory. It builds this large complex message and then fires it over the network. So I am basically sniffing the traffic and inspecting the packet and trying to hardcode the data it's sending over to see if I can get a response from the server this way. However I can't seem to replicate the message perfectly. Some of it, such as the license code it sends is in "clear hex", that is, hex that translates into ascii, where-as some other portions of the data are not "clear hex" such as "aa" which does not translate into ascii (or at least a common character set?? if that makes any sense I'm not sure).
Ideally I'd like to not do it like this, but it's a stepping stone to see if can get the server to respond to me. One of the functions is for the application to register itself and that's the one I am trying to replicate.
Some of my assumptions above may be wrong, so I apologize in advance. Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 中,所有“字符”数据都编码为 Unicode(而不是 ASCII)。因此,当你与外界交谈时,你需要将内部字符串映射到外界。有几种方法可以做到这一点:
使用
ByteArrayOutputStream
。这基本上是一个可以附加的不断增长的字节缓冲区。这允许您使用字节构建消息。使用
getBytes(encoding)
,其中编码是对方理解的编码。在您的情况下,文本部分将是“ASCII”。就您而言,您可能两者都需要。创建一个字节缓冲区,然后向其中附加字符串和字节,然后通过套接字 API 发送最终结果 (
getByteArray()
)。In Java, all "character" data is encoded as Unicode (and not ASCII). So when you talk to something outside, you need to map the internal strings to the outside world. There are several ways to do it:
Use a
ByteArrayOutputStream
. This is basically a growing buffer of bytes to which you can append. This allows you to build the message using bytes.Use
getBytes(encoding)
where encoding is the encoding the other side understands. In your case, that would be "ASCII" for the text parts.In your case, you probably need both. Create a byte buffer and then append strings and bytes to it and then send the final result (
getByteArray()
) via the socket API.