关于..我的'拆包商'的问题。 [英] question about .. my 'unpacker'.

查看:56
本文介绍了关于..我的'拆包商'的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将32位字打包成3-10位样本。所以现在:


Sample0对应于0-9位

Sample1对应于位10-19

Sample2对应于位20 -29

比特30和31不在乎


所以我的拆包工具看起来像:


void unpack(

unsigned int * beg,

unsigned int * end,

float * dest)

{

const unsigned int mask =(1<< 20)-1;

while(beg!= end)

{

* dest ++ = * beg&面具;

* dest ++ = * beg>> 20& ((1 << 10)-1);

++求;

}

}


用法:

unsigned int val(0xA5A5A5);

float dest [3];

unpack(& val,& ; val + 1,dest);


我想要一种 - 通用的方法,它可以用于花车和

双倍。我怀疑我也应该使用矢量,但是批评

和/或更精致的实现欢迎。


I''m trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don''t cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I''d like a - sort of - generic approach that''ll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.

推荐答案

ma******@pegasus.cc.ucf.edu 写道:

....
ma******@pegasus.cc.ucf.edu wrote:
....

我想要一种 - 通用的方法,它可以用于浮动和
双倍。


这样的事情?


模板< typename InT,typename OutT,unsigned N>

void unpack(

InT beg,

inT end,

OutT(& dest)[N]



{

const unsigned int mask =(1<< 20)-1 ;


unsigned i = 0;


while(beg!= end)

{

dest [i ++] = * beg&掩码;

if(i> = N)抛出溢出;


dest [i ++] = * beg>> 20& 〜mask;

如果(i> = N)抛出溢出;

++求助;

}

}


我怀疑我也应该使用矢量,但是批评和/或更精致的实现欢迎。

I''d like a - sort of - generic approach that''ll acount for floats and
doubles.
Somthing like this ?

template < typename InT, typename OutT, unsigned N >
void unpack(
InT beg,
InT end,
OutT ( & dest )[ N ]
)
{
const unsigned int mask=(1<<20)-1;

unsigned i = 0;

while (beg!=end)
{
dest[ i++ ] = *beg & mask;
if ( i >= N ) throw Overflow;

dest[ i++ ] = *beg >> 20 & ~mask;
if ( i >= N ) throw Overflow;
++beg;
}
}

I suspect I should also use a vector, nonetheless critiques and/or a more refined implementation welcome.



* ma******@pegasus.cc.ucf.edu

我正在尝试将32位字打包成3-10位样本。所以现在:

Sample0对应于位0-9
Sample1对应于位10-19
Sample2对应于位20-29
位30和31是否为我关心

所以我的解包器看起来像:

void unpack(
unsigned int * beg,
unsigned int * end,


''const''这两个论点。


float * dest)


神秘的选择结果类型。


{
const unsigned int mask =(1<< 20)-1;
while(beg!= end)
{
* dest ++ = * beg&面具;


这里你要复制低20位。


* dest ++ = * beg>> 20& ((1 <<;< 10)-1);


++求;
}
}

I''m trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don''t cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
''const'' for both those arguments.

float* dest)
mysterious choice of result type.

{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
Here you''re copying the lower 20 bits.

*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
}
}




-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


ma ****** @ pegasus.cc.ucf.edu 写道:
我正在尝试打开32位字转换为3-10位样本。所以现在:

Sample0对应于位0-9
Sample1对应于位10-19
Sample2对应于位20-29
位30和31是否为我关心

所以我的解包器看起来像:

void unpack(
unsigned int * beg,
unsigned int * end, float * dest)
{const unsigned int mask =(1<< 20)-1;
while(beg!= end)
{
* dest ++ = * beg&面具;
* dest ++ = * beg>> 20& ((1 << 10)-1);
++ beg;


这肯定不行。首先,它只给你两个值,而不是
三。

}
}

用法:
无符号int val(0xA5A5A5);
float dest [3];
unpack(& val,& val + 1,dest);

我想要一个 - 一种 - 通用的方法,它将重复浮动和
双倍。我怀疑我也应该使用矢量,但仍需要批评
和/或更精致的实施欢迎。
I''m trying to unpack a 32 bit word into 3-10 bits samples. So now:

Sample0 corresponds to bits 0-9
Sample1 corresponds to bits 10-19
Sample2 corresponds to bits 20-29
Bits 30 and 31 are don''t cares

So my unpacker looks like:

void unpack(
unsigned int *beg,
unsigned int *end,
float* dest)
{
const unsigned int mask=(1<<20)-1;
while (beg!=end)
{
*dest++ = *beg & mask;
*dest++ = *beg >> 20 & ((1<<10)-1);
++beg;
This certainly won''t work. For one, it only gives you two values, not
three.
}
}

Usage:
unsigned int val(0xA5A5A5);
float dest[3];
unpack (&val, &val+1, dest);

I''d like a - sort of - generic approach that''ll acount for floats and
doubles. I suspect I should also use a vector, nonetheless critiques
and/or a more refined implementation welcome.




#include< vector>

using namespace std;


typedef unsigned int uint;


template< typename DestType>

void unpack(vector< uint> :: const_iterator srcBegin,

vector< uint> :: const_iterator const srcEnd,

vector< DestType> :: iterator dest)

{

static const uint mask =(1<< 10)-1;

while(srcBegin ++!= srcEnd)

{

* dest ++ = DestType((* srcBegin)& mask);

* dest ++ = DestType((* srcBegin>> 10)&掩码);

* dest ++ = DestType((* srcBegin>> 20)& mask);

}

}


干杯! --M



#include <vector>
using namespace std;

typedef unsigned int uint;

template <typename DestType>
void unpack( vector<uint>::const_iterator srcBegin,
vector<uint>::const_iterator const srcEnd,
vector<DestType>::iterator dest )
{
static const uint mask = (1<<10)-1;
while( srcBegin++ != srcEnd )
{
*dest++ = DestType( (*srcBegin ) & mask );
*dest++ = DestType( (*srcBegin >> 10) & mask );
*dest++ = DestType( (*srcBegin >> 20) & mask );
}
}

Cheers! --M


这篇关于关于..我的'拆包商'的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆