Хорошо, это работает, но довольно уродливо помещать буфер в поток строк, так что вы можете использовать std :: getline с разделителем для извлечения битов, а затем использовать другой поток строк или std :: stoi и друзей для преобразовать элементы в нужные типы:
https://repl.it/repls/GainsboroInsecureEvents
void FromBuffer(unsigned char* b, int s){
std::string item;
std::stringstream ss((char *)b);
// You don't NEED to use std::stringstream to convert
// the item to the primitive types - you could use
// std::stoi, std::stol, std::stoll, etc but using a
// std::stringstream makes it so you don't need to
// know which primitive type the variable is
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ia;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ib;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ic;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> id;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ua;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ub;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> uc;
std::getline(ss,item,'\x1d'); std::stringstream(item) >> ud;
// Until you get to here. Then >> stops on a space
// and all the sudden you can't use >> to get the data
std::getline(ss,str,'\x1d');
// And a C string is even worse because you need to
// respect the length of the buffer by using strncpy
std::getline(ss,item,'\x1d'); strncpy(sz,item.c_str(),64); sz[63] = '\0';
}
Так что я думаю, что гораздо лучший способ - создать новый фасет ctype, который использует новый разделитель и наполнение потока строк новым фасетом, как здесь было сделано изменение разделителя для cin (c ++)
Таким образом, мы можем просто извлечь непосредственно, что НАМНОГО лучше:
https://repl.it/repls/GraveDraftyAdministrators
void FromBuffer(unsigned char* b, int s){
struct delimiter : std::ctype<char> {
delimiter() : std::ctype<char>(get_table()) {}
static mask const* get_table()
{
static mask rc[table_size];
rc[0x1d] = std::ctype_base::space;
return &rc[0];
}
};
std::stringstream ss((char *)b);
ss.imbue(std::locale(ss.getloc(), new delimiter));
ss >> ia
>> ib
>> ic
>> id
>> ua
>> ub
>> uc
>> ud
>> str
>> sz;
}