YARP
Yet Another Robot Platform
Vocab.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/os/Vocab.h>
8 
9 using namespace yarp::os;
10 
11 NetInt32 Vocab32::encode(const std::string& str)
12 {
13  char a = '\0';
14  char b = '\0';
15  char c = '\0';
16  char d = '\0';
17  if (str.length() >= 1) {
18  a = str[0];
19  if (str.length() >= 2) {
20  b = str[1];
21  if (str.length() >= 3) {
22  c = str[2];
23  if (str.length() >= 4) {
24  d = str[3];
25  }
26  }
27  }
28  }
29  return createVocab32(a, b, c, d);
30 }
31 
32 
33 std::string Vocab32::decode(NetInt32 code)
34 {
35  std::string str;
36  for (int i = 0; i < 4; i++) {
37  int ch = code % 256;
38  if (ch > 0) {
39  str += ((char)ch);
40  }
41  code /= 256;
42  }
43  return str;
44 }
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:11
std::string decode(NetInt32 code)
Convert a vocabulary identifier into a string.
Definition: Vocab.cpp:33
An interface to the operating system, including Port based communication.
std::int32_t NetInt32
Definition of the NetInt32 type.
Definition: NetInt32.h:29
constexpr yarp::conf::vocab32_t createVocab32(char a, char b=0, char c=0, char d=0)
Create a vocab from chars.
Definition: Vocab.h:27