1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <bitset>
#include <string>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <limits>
using namespace std;
string s = "";
void decToBinary(unsigned packet) {
std::bitset<21> d{packet} ;
cout << d <<endl;
s = d.to_string();
}
// Function to remove the leading zeros
string removeLeadingZeros(string num)
{
// traverse the entire string
for (int i = 0; i < num.length(); i++) {
// check for the first non-zero character
if (num[i] != '0') {
// return the remaining string
string res = num.substr(i);
return res;
}
}
return "0";
}
int main()
{
decToBinary(1950000);
s.insert (s.end() - 7,1,'1');
s.insert (s.end() - 14,1,'0');
string g = "1";
int i{s.size()};
int rep = i - 16;
s.replace(rep, 1,g);
std::ostringstream oss;
oss << std::hex << std::setw(8) << std::setfill('0') << std::bitset<32>{s}.to_ulong();
const auto asHex { oss.str() };
string removzero = removeLeadingZeros(asHex);
string r1 = removzero.substr(0, 2);
string r2 = removzero.substr(2, 2);
string r3 = removzero.substr(4, 2);
std::cout << removzero << '\n';
std::cout << r1 << '\n';
std::cout << r2 << '\n';
std::cout << r3 << '\n';
const unsigned char Sendbuff[] = {0x10,0x00,0xD6,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
return 0;
}
| |