utils.cpp
Go to the documentation of this file.
1 /*
2  * @brief sim_loc_utils contains a collection of utility functions for SIM Localization.
3  *
4  * Copyright (C) 2019 Ing.-Buero Dr. Michael Lehning, Hildesheim
5  * Copyright (C) 2019 SICK AG, Waldkirch
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *
24  * * Redistributions of source code must retain the above copyright
25  * notice, this list of conditions and the following disclaimer.
26  * * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  * * Neither the name of SICK AG nor the names of its
30  * contributors may be used to endorse or promote products derived from
31  * this software without specific prior written permission
32  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
33  * contributors may be used to endorse or promote products derived from
34  * this software without specific prior written permission
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
37  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
40  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * Authors:
49  * Michael Lehning <michael.lehning@lehning.de>
50  *
51  * Copyright 2019 SICK AG
52  * Copyright 2019 Ing.-Buero Dr. Michael Lehning
53  *
54  */
55 
56 #include "sick_scan/ros_wrapper.h"
57 #include <string>
58 #include <vector>
59 #include <boost/algorithm/hex.hpp>
60 #include <boost/algorithm/string.hpp>
61 
62 #include "sick_scan/utils.h"
63 
64 /*
65  * Converts and returns binary data to hex string
66  * @param[in] binary_data binary input data
67  * @return hex string
68  */
69 std::string sick_scan::Utils::toHexString(const std::vector<uint8_t> & binary_data)
70 {
71  std::string hex_string;
72  hex_string.reserve(binary_data.size() * 2);
73  boost::algorithm::hex(binary_data.begin(), binary_data.end(), std::back_inserter(hex_string));
74  return hex_string;
75 }
76 
82 std::string sick_scan::Utils::toAsciiString(const uint8_t* binary_data, int length)
83 {
84  std::stringstream out;
85  for(int n = 0; n < length; n++)
86  {
87  int val = (int)(binary_data[n] & 0xFF);
88  if ((val == 0x20) || (val >= 48 && val <= 57) || (val >= 65 && val <= 90) || (val >= 97 && val <= 122))
89  {
90  out << std::string(1,(char)(val & 0xFF));
91  /*char s[2] = {0};
92  sprintf(s, "%c", (char)(val & 0xFF));
93  out << s;*/
94  }
95  else
96  {
97  out << "\\x" << std::setfill('0') << std::setw(2) << std::hex << val;
98  }
99  }
100  return out.str();
101 }
102 
103 /*
104  * Shortcut to replace linefeeds by colon-separators
105  */
107 {
108  boost::replace_all(s, "\n", ", ");
109  boost::replace_all(s, ": , ", ": ");
110  while(s.find(" ") != std::string::npos)
111  boost::replace_all(s, " ", " ");
112 }
static std::string toAsciiString(const uint8_t *binary_data, int length)
Definition: utils.cpp:82
static std::string toHexString(const std::vector< uint8_t > &binary_data)
Definition: utils.cpp:69
static void flattenString(std::string &s)
Definition: utils.cpp:106
static sick_scan::SickScanCommonTcp * s


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed May 5 2021 03:05:48