- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Java 和 Big Endian Byte Order 格式将字节数组值写入文件中。现在我需要从 C++ 程序中读取该文件...
我正在写入文件的字节数组由三个字节数组组成,如下所述-
short employeeId = 32767;
long lastModifiedDate = "1379811105109L";
byte[] attributeValue = os.toByteArray();
我正在将 employeeId
、lastModifiedDate
和 attributeValue
一起写入单个字节数组,并将生成的字节数组写入一个文件中,然后我将使用我的 C++ 程序从文件中检索字节数组数据,然后将其反序列化以从中提取 employeeId
、lastModifiedDate
和 attributeValue
它。
下面是我的工作 Java 代码,它将字节数组值写入大端格式的文件中:
public class ByteBufferTest {
public static void main(String[] args) {
String text = "Byte Array Test For Big Endian";
byte[] attributeValue = text.getBytes();
long lastModifiedDate = 1289811105109L;
short employeeId = 32767;
int size = 2 + 8 + 4 + attributeValue.length; // short is 2 bytes, long 8 and int 4
ByteBuffer bbuf = ByteBuffer.allocate(size);
bbuf.order(ByteOrder.BIG_ENDIAN);
bbuf.putShort(employeeId);
bbuf.putLong(lastModifiedDate);
bbuf.putInt(attributeValue.length);
bbuf.put(attributeValue);
bbuf.rewind();
// best approach is copy the internal buffer
byte[] bytesToStore = new byte[size];
bbuf.get(bytesToStore);
writeFile(bytesToStore);
}
/**
* Write the file in Java
* @param byteArray
*/
public static void writeFile(byte[] byteArray) {
try{
File file = new File("bytebuffertest");
FileOutputStream output = new FileOutputStream(file);
IOUtils.write(byteArray, output);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
现在我需要使用下面的 C++ 程序从同一个文件中检索字节数组并将其反序列化以提取 employeeId
、lastModifiedDate
和 attributeValue
从中。我不确定 C++ 方面的最佳方法是什么。以下是我到目前为止的代码:
int main() {
string line;
std::ifstream myfile("bytebuffertest", std::ios::binary);
if (myfile.is_open()) {
uint16_t employeeId;
uint64_t lastModifiedDate;
uint32_t attributeLength;
char buffer[8]; // sized for the biggest read we want to do
// read two bytes (will be in the wrong order)
myfile.read(buffer, 2);
// swap the bytes
std::swap(buffer[0], buffer[1]);
// only now convert bytes to an integer
employeeId = *reinterpret_cast<uint16_t*>(buffer);
cout<< employeeId <<endl;
// read eight bytes (will be in the wrong order)
myfile.read(buffer, 8);
// swap the bytes
std::swap(buffer[0], buffer[7]);
std::swap(buffer[1], buffer[6]);
std::swap(buffer[2], buffer[5]);
std::swap(buffer[3], buffer[4]);
// only now convert bytes to an integer
lastModifiedDate = *reinterpret_cast<uint64_t*>(buffer);
cout<< lastModifiedDate <<endl;
// read 4 bytes (will be in the wrong order)
myfile.read(buffer, 4);
// swap the bytes
std::swap(buffer[0], buffer[3]);
std::swap(buffer[1], buffer[2]);
// only now convert bytes to an integer
attributeLength = *reinterpret_cast<uint32_t*>(buffer);
cout<< attributeLength <<endl;
myfile.read(buffer, attributeLength);
// now I am not sure how should I get the actual attribute value here?
//close the stream:
myfile.close();
}
else
cout << "Unable to open file";
return 0;
}
我已经专门将存储 Java 端设置为 big-endian,这意味着我知道我猜每个字节属于哪里。那么如何在将字节移动到每个值的正确位置时对其进行编码呢?现在我正在将它编码为 little-endian 我猜这不是我想要的...
我在某个地方读到我可以在 C++ 中使用 ntoh
来反序列化字节数组。不确定 htons
是否会比我拥有的更好的解决方案目前?..
如果是,那么我不确定如何在我当前的 C++ 代码中使用它?
谁能看一下 C++ 代码,看看我能做些什么来改进它,因为我认为它看起来效率不高?有没有更好的方法来反序列化字节数组并在 C++ 端提取相关信息?
最佳答案
如果 Java 和 C++ 代码是由您开发的,那么最好使用 Google Protocol Buffer (https://developers.google.com/protocol-buffers/docs/overview) 而不是编写您自己的序列化器/反序列化器。
如果您真的想编写自己的实现,最好的方法是编写一个接收字节流作为参数(例如作为构造函数参数)的缓冲区类,并制定一些访问方法 readShort/readLong/readInt/readByte ...并且只交换需要的字节。
class ByteBuffer{
explicit ByteBuffer(uint8_t* byteStream, uint16_t streamLength);
uint8_t readUInt8(uint16_t readPos)const {return m_byteStream[readPos];} // no conversion needed
uint16_t readUInt16(uint16_t readPos)const {
const uint8_t byteCount = 2;
union{
uint16_t u16;
uint8_t u8[byteCount];
}tmp;
for(uint8_t i=0; i<byteCount; ++i){
tmp.u8[i] = readUInt8(readPos+i*8);
}
return ntohs(tmp.u16); // do conversion
}
...
}
这里缺少缓冲区后面的读取检查。如果您的代码应该是可移植的,那么您可以使用 ntohl/ntohs(请参阅:http://forums.codeguru.com/showthread.php?298741-C-General-What-do-ntohl%28%29-and-htonl%28%29-actually-do)。如果您自己交换字节,那么您的代码是不可移植的(只能在 Little-Endian 机器上运行)。如果您使用 ntoh,那么它也可以在这样的机器上运行。
为了方便起见,我还会编写一个包装类,您可以在其中直接读写您的字段(例如 employeeId):
class MyBuffer{
uint16_t readEmployeeId()const{return m_Buffer.readuint16(EmployeeId_Pos);}
....
static const uint16_t EmployeeId_Pos = 0;
....
}
关于c++ - 如何在 C++ 中使用 Big Endian 格式而不是 Little Endian 来移动字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19373922/
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Big Theta Notation - what exactly does big Theta represent
我有一个作业要证明这些是对还是错: a) 150n^3 + 43n^2 + 50^n + 3 = Ω(n^5) b) n^10 + 30n^8 + 80n^6 = O(n^12) c) 55n + 3
我可以在 big.Int 上使用像 Text() 这样的方法,它工作正常,但是如果我返回一个 big.Int 然后使用“myfunc().Text()”会抛出一个错误,而如果我返回一个 * big.I
我正在用 PHP 开发一个网络应用程序,此时核心库的大小为 94kb。虽然我认为我现在是安全的,但多大才算太大?脚本的大小是否会成为一个问题,如果是这样,可以通过将脚本拆分为多个库来改善这一点吗? 我
我正在复习 Big-Oh 符号,但我在理解这个问题的解决方案时遇到了问题: Is 2n + 10 ≡ O(n)? Can we find c and n0? 2n + 10 = 10 n >= 10/
我最近陷入了争论/辩论中,我试图对正确的解决方案做出明确的判断。 众所周知, n! grows very quickly ,但究竟有多快,足以“隐藏”可能添加到其中的所有其他常量? 让我们假设我有这个
我很难找出这段代码的 Big-O 符号。 我需要找到两个 for 循环的符号。 public static int fragment(int n) { int sum = 0; for (in
给定两个函数: f(n)=O(log2n) 和 g(n)=O(log10n) 其中一个是否支配另一个? 最佳答案 请记住,任何碱基的对数都可以转换为仅以常数变化的公共(public)碱基。 因此它们都
经过修改,我们得出结论,时间复杂度实际上是O(2^n) 问题是时间复杂度是多少?是 O(2^n) 还是? 我相信这是因为 for 循环被认为运行了 n 次。然后嵌套的 while 循环运行 2^n 次
以下嵌套循环的 Big-O 时间复杂度是多少: for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) {
我很想知道经验丰富的 C 程序员认为可以按值传递的参数大小的上限是什么。 上下文:我有机会使用 2×2 矩阵,它位于一个结构体中: typedef struct { double a, b, c,
我很想知道经验丰富的 C 程序员认为可以按值传递的参数大小的上限是什么。 上下文:我有机会使用 2×2 矩阵,它位于一个结构体中: typedef struct { double a, b, c,
假设我们有一个问题,我们使用 X 算法实现了 O(n) 或 O(log n) 或 etc...。 n 的值何时大到我们必须考虑替代实现?让我们看看我是否可以更好地解释自己。 For n=10,000
这属于哪种 Big-O 表示法?我知道 setSearch() 和 removeAt() 是 O(n) 的顺序(假设它们是任意一种)。我知道如果没有 for 循环它肯定是 O(n),但是我很困惑如何计
这是我的问题,我已经设法为 a 部分提出了一个答案,但对于 b 部分,我对 b 部分的答案并不是很自信。 在最近的一起法庭案件中,一名法官以蔑视城市为由,下令第一天罚款 2 美元。 之后的每一天,直到
我正在尝试计算以下算法的大 O,但我很困惑,需要一些帮助: Algorithm 1. DFS(G,n) Input: G- the graph n- the current node 1
我们有一个使用 F5 BIG-IP 服务器进行负载平衡的潜在客户端。在确定我们是否可以将我们的产品与他们的负载均衡器干净地集成时,我开始查看 F5 提供的 API。问题是,如果没有 F5 服务器,我无
我正在尝试使用 react-big-calendar 包。 http://intljusticemission.github.io/react-big-calendar/examples/index.
我的任务是尝试找到给定 Java 方法的 big-O 和 big-Omega,但不知道如何找到。我知道 big-O 给出了上限,big-Omega 给出了下限,但是在查看程序(更不用说递归程序)时,我
我正在尝试确定以下陈述是对还是错。 如果 f(n) ∈ O(n) 且 g(n) ∈ Ω(n),则 f(n) + g(n) ∈ Θ(n)。 我想我理解添加相同的渐近 big-O。 O(n) + O(n)
我是一名优秀的程序员,十分优秀!