// calculate the number of check digits for encode
auto cal(size_t sz) -> decltype(auto)
{
decltype(sz) k = 0;
decltype(sz) cur = 1;
while (cur - 1 < sz + k)
{
cur <<= 1;
k++;
}
return k;
}
编码
// encode
bool encode(const string &data, string &str)
{
str.clear();
auto check = cal(data.size());
str.resize(data.size() + check);
for (decltype(str.size()) i = 0, j = 0, p = 0; i != str.size(); i++)
{
if ((i + 1) == pow(2, p) && p < check)
{
str[i] = '0';
p++;
}
else if (data[j] == '0' || data[j] == '1')
{
str[i] = data[j++];
}
else
{
return false;
}
}
for (auto i = 0; i != check; i++)
{
int count = 0, index = 1 << i;
for (auto j = index - 1; j < str.size(); j += index)
{
for (auto k = 0; k != index && j < str.size(); k++, j++)
{
count ^= str[j] - '0';
}
str[index - 1] = '0' + count;
}
}
return true;
}
计算解码校验位
// calculate the number of check digits for decode
auto antical(size_t sz) -> decltype(auto)
{
decltype(sz) k = 0;
decltype(sz) cur = 1;
while (cur < sz)
{
cur <<= 1;
k++;
}
return k;
}
解码
// decode
auto decode(string &data, string &str) -> decltype(auto)
{
data.clear();
auto check = antical(str.size());
data.resize(str.size() - check);
decltype(data.size()) sum = 0;
for (decltype(check) i = 0; i != check; i++)
{
int pAnti = 0;
decltype(check) index = 1 << i;
for (decltype(str.size()) j = index - 1; j < str.size(); j += index)
{
for (auto k = 0; k < index && j < str.size(); j++, k++)
{
pAnti ^= str[j] - '0';
}
}
sum += pAnti << i;
}
if (sum != 0)
{
str[sum - 1] = (1 - (int)(str[sum - 1] - '0')) + '0';
}
for (decltype(str.size()) i = 0, j = 0, k = 0; i != str.size(); i++)
{
if ((i + 1) == (1 << j) && j < check)
{
j++;
}
else
{
data[k++] = str[i];
}
}
return sum;
}
测试结果
int main()
{
string source, dest;
while (cin >> source)
{
if (encode(source, dest))
{
cout << "source : " << source << endl;
cout << "dest : " << dest << endl;
}
size_t index;
cout << "input error index : ";
cin >> index;
auto check = dest.size();
if (index != 0 && index <= dest.size())
{
dest[index - 1] = (1 - (int)(dest[index - 1] - '0')) + '0';
}
cout << "code : " << dest << endl;
auto ret = decode(source, dest);
if (ret == 0)
{
cout << "source : " << source << endl;
cout << "dest : " << dest << endl;
}
else
{
cout << "error index : " << ret << endl;
cout << "corret source : " << source << endl;
cout << "corret dest : " << dest << endl;
}
cout << endl;
}
return 0;
}
结果
Verilog实现
.v功能代码
编码
module ham_encoder(
input wire clk,
input wire rst_n,
input wire data_v,
input wire [ 7:0] data_in,
output reg encode,
output reg [11:0] data_out
);
always @ (posedge clk or negedge rst_n) begin
if (!rst_n) begin
encode <= 1'b0;
end else begin
encode <= 1'b1;
end
end
always @ (posedge clk or negedge rst_n) begin
if (!rst_n) begin
data_out <= 12'b0;
end else if (data_v) begin
data_out[11:4] <= data_in[7:0];
data_out[3] <= data_in[7]^data_in[5]^data_in[3]^data_in[2];
data_out[2] <= data_in[7]^data_in[6]^data_in[4]^data_in[2]^data_in[1];
data_out[1] <= data_in[7]^data_in[6]^data_in[5]^data_in[3]^data_in[1]^data_in[0];
data_out[0] <= data_in[6]^data_in[4]^data_in[3]^data_in[0];
end else begin
data_out <= 12'b0;
end
end
endmodule
1.简介
iSCSI:Internet Small Computer System Interface,Internet小型计算机系统接口,又称为IP-SAN,是一种基于因特网及SCSI-3协议下的存储技术。
2.iSCSI的作用
基于客户端和服务端架构的虚拟磁盘技术,服务端提供…
Paper: Traffic Matrix Estimation Techniques- A Survey on Current Practices | IEEE Conference Publication | IEEE Xplore
来源:2023 International Conference on Sustainable Computing and Data Communication Systems (ICSCDS)
(强烈建议搭配英文原文看&…