转换我的代码到matlab

3次查看(最近30天)
默罕默德·贾马尔
评论道: Rena伯曼 2020年10月12日
大家好,我想把我的c++代码转换成matlab代码,我只想做一次,我是matlab新手(通常不使用matlab),谁能帮助我如何在matlab中实现它?谢谢你很多。
c++ code:(什么代码是返回一个特定的子字符串后发现syncword的子字符串在给定字符串)
使用MatchVec = std::向量匹配< >
uint16_tstd::uint8_t crcData, std::uint16_t crcReg
intPacketIndex = 0
模板< typename T,无符号N >
constexprunsigned count (T const (&)[N])无例外
返回N;
MatchVecconst std::string &str, const std::string &pattern
MatchVec矢量;
匹配m;
Std::string::size_type索引,pos = 0;
(index = str.find(pattern, pos)) != std::string::npos) {
m.first =指数;/ / startof Syncword
指数+ = pattern.length ()
m.second = str.substr(指数,264 + 16);/ / 264有效载荷的大小和两个字节的CRC
vec.push_back (m);
pos =指数;
返回矢量;
uint16_tcallccrc (uint8_t crcData, uint16_t crcReg)
uint8_t我;
(i = 0;我< 8;我+ +)
如果(((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
crcReg = (crcReg << 1) ^ CRC16_POLY;
其他的
crcReg = (crcReg << 1);
crcData< < = 1
返回crcReg;
无符号int valueOf(const std::string &substring) {
字符retChar = ' \ 0 '
Std::uint8_t total = 0;
int计数器= 1
(int i = byteSize;我> 0;我(){
如果(substring(张)= =' 1 ')总+ =计数器
如果(substring(张)! =' ')计数器* = 2
返回总;
无效const std::string &rx) {
无符号int ByteConvertRxBuffer [35] = {0}
uint8_trxBuffer [countof (ByteConvertRxBuffer)]
size_tireal = 0;/ /注意,最后两个字节用于CRC,这表示最后两个索引是CRC0的CRC值, CRC1
uint16_t校验和;/ / CRC的2字节即16位。
保龄球crcOK = false
uint8_t
校验和= CRC_INIT;
std:: cout < <“{”
(size_t我= 0;我< rx.length ();我+ = byteSize) {
字符咬(byteSize)
(int j = 0;j < byteSize;[J] = rx[i+ J]; [J] = rx[i+ J];
std:: cout < <返回对象的值(咬);/ /打印每个包内每个字节-8位的值打交道,每个包分开。
ByteConvertRxBuffer [ireal + +] =返回对象的值(咬);
如果(i+byteSize != rx.length()) std::cout<", "
std:: cout < <“}”< < endl;
(int i = 0;我< countof (ByteConvertRxBuffer);我+ +)
rxBuffer[我]= ByteConvertRxBuffer[我];
(i = 0;我< sizeof (rxBuffer);我+ +)
checksum = calcCRC(rxBuffer[i], checksum);
PacketIndex + +;
如果(校验和= = 0)
crcOK = true;
std:: cout < <“您的包号”< < PacketIndex < <”:“<<“有效的”< < endl;
其他的
crcOK = false;
std:: cout < <"你的包号"< < PacketIndex < <”:“<<“无效”< < endl;
intMain () /*int Main (int argc, char** argv){}*/
std:: string PacketData =“011101111001001010101010101010101010101010101100100110000101101010001110111100010000000000000011001010010000010111000011011001100100101110101100100101000011110110110001100110000111100110001100110000010111101111010000101101011100110011000000101101111110100010111100010100110000111111010110000101101111110011001111001011000011001001001100000001000100101101000110000010000111111110111111110”
std:: string SyncWord =“10010011000010110101000111011110”;/ / syncword模式
size_t notfound = PacketData.find(SyncWord);
Assert (notfound != string::npos);//如果没有找到出口和不完整的。
汽车匹配= findPattern(PacketData, SyncWord)
(auto &match: match) {
std:: cout < <“匹配位置:”< <匹配。第一个< < std:: endl;
std:: cout < <“字符串”< <匹配。第二个< < std:: endl;
deBin(匹配。二);
返回0;
2的评论
Rena伯曼
Rena伯曼 2020年10月12日
(回答Dev)恢复编辑

登录评论。

答案(1)

图像分析
图像分析 8月6日
你不需要预先声明变量的类型(uint8, int等)。
开始循环时不需要大括号,而循环以"end"关键字结束。取代
结束
取代
/ *注释...
for循环通常有这样的格式:
startingValue: stepValue: endingValue
用括号替换括号:
substring(张)
substring(张)
! = ~ = 在MATLAB。
除此之外,您可以尝试它并在遇到错误时处理它们。这是学习MATLAB语法的好方法。
对于位移位,请查看bitshift()和一整套以“bit”开头的位函数。
2的评论
图像分析
图像分析 8月9日
不。你删掉了原来的问题。我现在为什么要帮你,我怎么能帮你?

登录评论。

社区寻宝

在MATLAB中心找到宝藏,并发现社区如何可以帮助你!

开始狩猎!