有没有办法从十六进制private void ProcessFrame(byte[] buffer)
{
byte[] bufferCopy = (byte[])buffer.Clone();
double[,,] XYZArray = new double[IMGH, IMGW, 3];
byte[,] DepthArray = new byte[IMGH, IMGW];
byte[,,] RGBArray = new byte[IMGH, IMGW, 3];
for (int i = 0; i < IMGW; i++)
{
for (int j = 0; j < IMGH; j++)
{
int index = (i * IMGW + j) * 20;
//byte[] arr = {bufferCopy[index], bufferCopy[index + 1], bufferCopy[index + 2], bufferCopy[index + 3] };
float v = System.BitConverter.ToSingle(bufferCopy, index);
if (!float.IsInfinity(v) && !float.IsNaN(v))
{
XYZArray[i, j, 0] = v;
}
//arr = new byte[]{bufferCopy[index + 4], bufferCopy[index + 5], bufferCopy[index + 6], bufferCopy[index + 7] };
v = System.BitConverter.ToSingle(bufferCopy, index + 4);
if (!float.IsInfinity(v) && !float.IsNaN(v))
{
XYZArray[i, j, 1] = v;
}
v = System.BitConverter.ToSingle(bufferCopy, index + 8);
if (!float.IsInfinity(v) && !float.IsNaN(v))
{
XYZArray[i, j, 2] = v;
}
// Debug.Log("for loop called");
DepthArray[i, j] = bufferCopy[index + 12];
RGBArray[i, j, 2] = bufferCopy[index + 16]; // B
RGBArray[i, j, 1] = bufferCopy[index + 17]; // G
RGBArray[i, j, 0] = bufferCopy[index + 18]; // R
}
}
this.EnQueue(XYZArray);
}
private void EnQueue(System.Object obj)
{
lock (queueLock)
{
queue.Enqueue(obj);
}
}
private bool DeQueue(System.Object outObj)
{
bool success = false;
lock (queueLock)
{
if (queue.Count > 0)
{
outObj = queue.Dequeue();
success = true;
}
}
return success;
}
public int lengthOfQueue()
{
int count = -1;
lock (queueLock)
{
count = queue.Count;
}
return count;
}
public double[,,] getXYZArray()
{
double[,,] retVal = new double[,,] { };
this.DeQueue(retVal);
return retVal;
}
或std::bitset
构建std::string
,反之亦然而不执行二进制移位操作?我知道如何这样做,但我想知道是否可以使用C ++流或类似的东西来做到这一点。
这是我的代码到目前为止(试图避免遭到版主的抨击):
QString
答案 0 :(得分:1)
您可以将十六进制字符串转换为整数,并从中构造一个bitset。
#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
using namespace std;
int main()
{
string s = "0xA";
stringstream ss;
ss << hex << s;
unsigned n;
ss >> n;
bitset<32> b(n);
// outputs "00000000000000000000000000001010"
cout << b.to_string() << endl;
}
答案 1 :(得分:0)
基于Trevor下面提供的答案,我写了以下代码:
use_frameworks!
我希望对寻求相同解决方案的其他人有用。
答案 2 :(得分:0)
这个怎么样?
std::string str("deadBEEF");
std::bitset<128> bits; // result
char* raw = reinterpret_cast<char*>(&bits) + sizeof(bits) - 1; // last byte
assert(str.size() <= 2 * sizeof(bits));
for (size_t ii = 0; ii < str.size(); ++ii) {
char ch = str[ii];
if (ch >= '0' && ch <= '9') {
ch -= '0';
} else if (ch >= 'a' && ch <= 'f') {
ch -= 'a' - 10;
} else if (ch >= 'A' && ch <= 'F') {
ch -= 'A' - 10;
} else {
throw std::runtime_error("invalid input");
}
if (ii % 2 == 0) {
ch <<= 4; // nibble
}
*raw |= ch;
if (ii % 2) {
--raw;
}
}
cout << bits << endl;
以上假设std::bitset
只有一个数据成员:一个足够大的整数数组,用于保存模板化的位数。我认为这是一个公平的假设,但肯定不能保证便携。好消息是它会很快 - 部分是因为它没有动态内存分配。
答案 3 :(得分:0)
QBitArray BitConverter::GetBits(quint64 value, bool lsb)
{
QString binary;
binary.setNum(value, 2);
QBitArray bits(binary.count());
if (lsb)
{
for (int i = 0; i < binary.count(); i++)
{
bits[i] = binary[i] == '1';
}
}
else
{
for (int i = binary.count() - 1, j = 0; i >= 0; i--, j++)
{
bits[j] = binary[i] == '1';
}
}
return bits;
}