📄 uvlc.cpp
字号:
/*UVLC编码*/
#include "stdio.h"
#include "iostream.h"
#include "math.h"
#include "string.h"
#define K 10
int pow1(int x,int y)
{
int t;
t=x;
if(y==0)
return 1;
else if(y==1)
return t;
while(y>1)
{
t=t*x;
y--;
}
return t;
}
void main()
{
int s1[K]={250,20,0,89,250,78,77,79,22,510};
int y=0,sum=0,z;
int code[200];
int te[10];
int temp[10];
int i,n,L,INFO,j,k,t=0,l,info;
for(i=0;i<K;i++)
cout<<s1[i]<<'\t';
cout<<endl;
for(i=0;i<K;i++)
{
if(s1[i]==0) /*为0时做特别处理*/
n=-1;
else
{
n=log10((s1[i]+1)/2)/log10(2);
}
L=2*(1+n)+1;/*编码位长*/
/*编码的信息*/
for(j=1;j<9;j++)
if(((pow1(2,j)-1)<=s1[i])&&((pow1(2,j+1)-1)>s1[i]))
{
INFO=s1[i]-pow1(2,j)+1;
break;
}
k=0;
temp[k]=1;
/*建立码表*/
if(L>1)
{
for(j=0;j<L-1;j+=2)
{
k++;
temp[k++]=INFO%2;
INFO=INFO/2;
temp[k]=0;
}
}
for(j=k;j>=0;j--)
{
// cout<<temp[j];
code[t++]=temp[j];
cout<<temp[j];
}
cout<<endl;
}
for(i=0;i<t;i++)
cout<<code[i];
cout<<endl;
i=0;
y=0;
sum=0;
k=0;
/*解码过程*/
while(i<t)
{
l=0;
for(y=i;code[y]!=1;y+=2);
sum=0;
z=0;
for(j=y-1;j>i;j-=2)
{
sum+=code[j]*pow1(2,z++);//求信息值
}
l=y-i+1;//求码长
l=l/2;
info=sum;
te[k++]=pow1(2,l)+info-1;//求值N
i=y+1;
}
//输出码值
for(j=0;j<k;j++)
cout<<te[j]<<'\t';
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -