2010-09-14 16:43:00 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <iostream>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/VarInt.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/ReadBufferFromString.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2010-09-14 16:43:00 +00:00
|
|
|
#include <Poco/HexBinaryEncoder.h>
|
|
|
|
|
|
|
|
|
2017-06-07 19:05:15 +00:00
|
|
|
static void parse_trash_string_as_uint_must_fail(const std::string & str)
|
|
|
|
{
|
|
|
|
using namespace DB;
|
|
|
|
|
|
|
|
unsigned x = 0xFF;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
x = parse<unsigned>(str);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// Ok
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "Parsing must fail, but finished sucessfully x=" << x;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-14 16:43:00 +00:00
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2017-06-07 19:05:15 +00:00
|
|
|
parse_trash_string_as_uint_must_fail("trash");
|
|
|
|
parse_trash_string_as_uint_must_fail("-1");
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << std::endl
|
|
|
|
<< argv[0] << " unsigned_number" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::UInt64 x = DB::parse<UInt64>(argv[1]);
|
|
|
|
Poco::HexBinaryEncoder hex(std::cout);
|
|
|
|
DB::writeVarUInt(x, hex);
|
|
|
|
std::cout << std::endl;
|
2012-12-14 19:41:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string s;
|
2012-12-14 19:41:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
DB::WriteBufferFromString wb(s);
|
|
|
|
DB::writeVarUInt(x, wb);
|
|
|
|
wb.next();
|
|
|
|
}
|
2012-12-14 19:41:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
hex << s;
|
|
|
|
std::cout << std::endl;
|
2012-12-14 19:41:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
s.clear();
|
|
|
|
s.resize(9);
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
s.resize(DB::writeVarUInt(x, s.data()) - s.data());
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
hex << s;
|
|
|
|
std::cout << std::endl;
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::UInt64 y = 0;
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DB::ReadBufferFromString rb(s);
|
|
|
|
DB::readVarUInt(y, rb);
|
2012-12-14 19:41:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::cerr << "x: " << x << ", y: " << y << std::endl;
|
2016-08-05 02:41:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0;
|
2010-09-14 16:43:00 +00:00
|
|
|
}
|