YARP
Yet Another Robot Platform
Vocab.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include <yarp/os/Vocab.h>
11 
12 using namespace yarp::os;
13 
14 NetInt32 Vocab::encode(const std::string& str)
15 {
16  char a = '\0';
17  char b = '\0';
18  char c = '\0';
19  char d = '\0';
20  if (str.length() >= 1) {
21  a = str[0];
22  if (str.length() >= 2) {
23  b = str[1];
24  if (str.length() >= 3) {
25  c = str[2];
26  if (str.length() >= 4) {
27  d = str[3];
28  }
29  }
30  }
31  }
32  return createVocab(a, b, c, d);
33 }
34 
35 
36 std::string Vocab::decode(NetInt32 code)
37 {
38  std::string str;
39  for (int i = 0; i < 4; i++) {
40  int ch = code % 256;
41  if (ch > 0) {
42  str += ((char)ch);
43  }
44  code /= 256;
45  }
46  return str;
47 }
yarp::os::createVocab
constexpr yarp::conf::vocab32_t createVocab(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:22
yarp::os::Vocab::decode
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:36
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
Vocab.h
yarp::os::NetInt32
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:33