Java 字节缓冲区到 C

发布于 2024-07-24 22:06:48 字数 310 浏览 5 评论 0原文

从 Windows 上的 C 程序中,我们需要像 Java 字节缓冲区一样读取和写入,该字节缓冲区在 BIG_ENDIAN 中存储二进制文件。

算法描述如下: http://mindprod.com/jgloss/binaryformats.html

需要读写 int 和漂浮。

有谁有执行此操作的示例 c 或 C++ 代码或参考吗?

TIA, 伯特

From a C program on Windows we need to read and write like a Java bytebuffer which stores binary in BIG_ENDIAN

The algorithm is described at :
http://mindprod.com/jgloss/binaryformats.html

Need to read and write int and float.

Does anyone have example c or C++ code that does this or a reference ?

TIA,
Bert

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-07-31 22:06:48

我认为困难在于大端和小端之间的转换。

本文应该可以帮助您解决 Endian 转换问题。 它包含用于交换整数、长整数、浮点数和任意长度字节数组的字节顺序的代码。
http://www.codeproject.com/KB/cpp/endianness.aspx

交换任意类型的代码如下所示:

#include <algorithm> //required for std::swap

#define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))

void ByteSwap(unsigned char * b, int n)
{
   register int i = 0;
   register int j = n-1;
   while (i<j)
   {
      std::swap(b[i], b[j]);
      i++, j--;
   }
}

I assume the difficulty is in converting between Big Endian and Little Endian.

This article should help you out with the Endian conversions. It contains code to swap the byte order on integers, long integers, floating point numbers, and byte arrays of arbitrary length.
http://www.codeproject.com/KB/cpp/endianness.aspx

The code to swap an arbitrary type looks like this:

#include <algorithm> //required for std::swap

#define ByteSwap5(x) ByteSwap((unsigned char *) &x,sizeof(x))

void ByteSwap(unsigned char * b, int n)
{
   register int i = 0;
   register int j = n-1;
   while (i<j)
   {
      std::swap(b[i], b[j]);
      i++, j--;
   }
}
赏烟花じ飞满天 2024-07-31 22:06:48

您想使用 htonl 和类似的。 其余的设计是你自己的。

You want to use htonl and similar. The rest of design is your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文