Skip to content

Commit ba393a7

Browse files
committed
Version 2
- Rewritten whole library - Improved preformace - Changed function names - Improved meson framework - Fixed issues in Readme.md - Added make file - Added CI for auto testing - Added tests - Added examples - Fixed issues in `Readme.md`
1 parent 6009a3e commit ba393a7

23 files changed

+729
-194
lines changed

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
language: generic
2+
sudo: enabled
3+
services:
4+
- docker
5+
stages:
6+
- test
7+
jobs:
8+
include:
9+
stage: test
10+
install:
11+
- sudo docker pull slocomptech/docker-meson-cpp:latest
12+
- sudo docker run -it -d --name buildenv slocomptech/docker-meson-cpp bash
13+
- sudo docker ps
14+
- sudo docker exec buildenv apt-get install git -y
15+
before_script:
16+
- sudo docker exec buildenv git clone -b ${TRAVIS_BRANCH} https://github.com/SloCompTech/ByteConvert_cpp.git ByteConvert_cpp
17+
script:
18+
- sudo docker exec buildenv bash -c 'cd ByteConvert_cpp && meson build'
19+
- sudo docker exec buildenv bash -c 'cd ByteConvert_cpp && ninja -C build'
20+
- sudo docker exec buildenv bash -c 'cd ByteConvert_cpp && ninja -C build test'
21+
- sudo docker exec buildenv bash -c 'cd ByteConvert_cpp && ninja -C build install'
22+
after_script:
23+
- sudo docker stop buildenv
24+
- sudo docker rm buildenv
25+
- sudo docker rmi slocomptech/docker-meson-cpp:latest
26+
- sudo docker ps -a

ByteConvert/ByteConvert.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* @file ByteConvert.cpp
3+
*
4+
* @authors Martin Dagarin
5+
* @date 22/07/2018
6+
*
7+
* @brief Source file of ByteConvert module
8+
*/
9+
10+
#include "ByteConvert.hpp"
11+
12+
namespace ByteConvert
13+
{
14+
std::string byte_to_hex(const uint8_t& byte)
15+
{
16+
std::string buffer("");
17+
uint8_t val;
18+
19+
for (uint8_t i = 0; i < 2; i++) {
20+
if (i == 0)
21+
val = (byte >> 4);
22+
else
23+
val = byte & 0x0f;
24+
if (val >= 16)
25+
buffer += '?';
26+
else if (val < 10)
27+
buffer += ('0' + val);
28+
else
29+
buffer += ('W' + val);
30+
}
31+
return buffer;
32+
}
33+
34+
uint8_t hex_to_byte(const std::string& hex)
35+
{
36+
uint8_t val = 0x00;
37+
uint8_t pos = (hex.size() > 1) ? hex.size() - 2 : hex.size() - 1;
38+
char c;
39+
for (; pos < hex.size(); pos++) {
40+
c = hex[pos];
41+
if (c >= 'A' && c <= 'F')
42+
val += (c - '7');
43+
else if (c >= 'a' && c <= 'f')
44+
val += (c - 'W');
45+
else if (c >= '0' && c <= '9')
46+
val += (c - '0');
47+
else
48+
val += '?';
49+
if (pos == 0 && hex.size() > 1)
50+
val = (val << 4);
51+
}
52+
return val;
53+
}
54+
55+
std::string block_to_hex_string(const uint8_t* const block, const size_t& size)
56+
{
57+
std::string buffer("");
58+
for (size_t i = 0; i < size; i++) {
59+
buffer += byte_to_hex(block[i]);
60+
}
61+
return buffer;
62+
}
63+
64+
uint8_t* hex_string_to_block(const std::string& hex_string, size_t* const size)
65+
{
66+
size_t block_size = hex_string.size()/2 + ((hex_string.size() % 2 == 1) ? 1 : 0);
67+
uint8_t* block = new uint8_t[block_size];
68+
int offset = 0;
69+
for (size_t i = 0;i < block_size; i++) {
70+
if (i == 0 && hex_string.size() % 2 == 1) {
71+
offset = -1;
72+
block[i] = hex_to_byte(std::string("0") + hex_string[0]);
73+
continue;
74+
}
75+
block[i] = hex_to_byte(std::string(1,hex_string[i*2 + offset]) + hex_string[i*2 + 1 + offset]);
76+
}
77+
if (size != nullptr)
78+
*size = block_size;
79+
return block;
80+
}
81+
}

ByteConvert/ByteConvert.hpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @file ByteConvert.hpp
3+
*
4+
* @authors Martin Dagarin
5+
* @date 22/07/2018
6+
*
7+
* @brief Header of ByteConvert module
8+
*/
9+
10+
#ifndef _ByteConvert_hpp_
11+
#define _ByteConvert_hpp_
12+
13+
#include <string>
14+
#include <cstdint>
15+
16+
17+
/**
18+
* @brief Namespace which holds all ByteConvert functions
19+
*/
20+
namespace ByteConvert {
21+
/**
22+
* @brief Converts single byte to hex string
23+
*
24+
* @param[in] byte Byte to convert to hex string
25+
* @return Hex string
26+
*/
27+
std::string byte_to_hex(const uint8_t& byte);
28+
29+
/**
30+
* @brief Converts hex string (1 or 2 chars) to byte
31+
*
32+
* @param[in] hex Hex string (1 or 2 chars) to convert to byte
33+
* @return Byte
34+
*/
35+
uint8_t hex_to_byte(const std::string& hex);
36+
37+
/**
38+
* @brief Converts array of bytes to hex string
39+
*
40+
* @param[in] block Array of bytes
41+
* @param[in] size Size of block
42+
* @return Hex string
43+
*/
44+
std::string block_to_hex_string(const uint8_t* const block, const size_t& size);
45+
46+
/**
47+
* @brief Converts hex string to array of bytes
48+
*
49+
* @param[in] hex_string Hex string
50+
* @param[out] size Size of returned array
51+
* @return Array of bytes
52+
*/
53+
uint8_t* hex_string_to_block(const std::string& hex_string,size_t* const size);
54+
55+
/**
56+
* @brief Converts numeric without floating point variables (uses sizeof()) to array of bytes
57+
*
58+
* @param[in] variable Variable to convert to bytes
59+
* @param[out] size Size of array (if pointer specifed)
60+
* @return Array of bytes
61+
*/
62+
template<class T>
63+
uint8_t* to_block(T variable,size_t* const size)
64+
{
65+
uint8_t block_size = sizeof(T)/sizeof(uint8_t);
66+
uint8_t *block = new uint8_t[block_size];
67+
for (size_t i = 0; i < block_size; i++) {
68+
if (i != 0) variable >>= 8;
69+
block[block_size - 1 - i] = (uint8_t)(variable & 0xff);
70+
}
71+
if (size != nullptr)
72+
*size = block_size;
73+
return block;
74+
}
75+
76+
/**
77+
* @brief Converts byte to variable
78+
*
79+
* @param[in] block Array of bytes
80+
* @return Variable
81+
*/
82+
template<class T>
83+
T to_variable(const uint8_t* block)
84+
{
85+
size_t var_size = sizeof(T)/sizeof(uint8_t);
86+
T var = 0x00;
87+
for (size_t i = 0; i < var_size; i++) {
88+
if (i != 0) var <<= 8;
89+
var |= block[i];
90+
}
91+
return var;
92+
}
93+
94+
/**
95+
* @brief Converts byte to variable
96+
*
97+
* @param[in] block Array of bytes
98+
* @return Variable
99+
*/
100+
template<class T>
101+
T to_variable(const uint8_t* block, const size_t& block_size)
102+
{
103+
T var = 0x00;
104+
for (size_t i = 0; i < block_size; i++) {
105+
if (i != 0) var <<= 8;
106+
var |= block[i];
107+
}
108+
return var;
109+
}
110+
}
111+
112+
#endif
File renamed without changes.

CHANGELOG

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 2.0.0
2+
- Rewritten whole library
3+
- Improved preformace
4+
- Changed function names
5+
- Improved meson framework
6+
- Fixed issues in Readme.md
7+
- Added make file
8+
- Added CI for auto testing
9+
- Added tests
10+
- Added examples
11+
- Fixed issues in `Readme.md`

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build:
2+
meson build
3+
ninja -C build
4+
ninja -C build test
5+
install:
6+
ninja -C build install
7+
clean:
8+
rm -rf build

README.md

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,51 @@
11
<p align="center"><img src="extras/graphics/512.png" alt="Byteconvert" height="200px"></p>
22

33
# ByteConvert
4+
45
[![Build Status](https://travis-ci.org/SloCompTech/ByteConvert_cpp.svg?branch=master)](https://travis-ci.org/SloCompTech/ByteConvert_cpp)
56
[![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg)]()
67

78
## What's about ?
8-
Have you ever wanted to transmit `int`,`short`,`long`,`double` or any other numeric type over I2C,SPI,serial or other protocol or bus, but you converted variable to string to be able to transmit it char by char. This library enables you to convert any numeric value to bytes or other way around and you can also print array of bytes.
9+
10+
Have you ever wanted to transmit `int`,`short`,`long` or any other numeric type over I2C,SPI,serial or other protocol or bus, but you converted variable to string to be able to transmit it char by char. This library enables you to convert any numeric value to bytes or other way around and you can also print array of bytes.
911

1012
## What you need to consider, when you are using this library
13+
1114
When you are using this library, you need to consider variable byte size, because if you are using different platforms, then there may be some errors, because int on platform 1 has 4 bytes and int on platform 2 may has 2 bytes.
1215

1316
## What you need to install this project ?
14-
You need [python3](https://www.python.org/), [ninja](https://ninja-build.org/) and [meson](http://mesonbuild.com).
15-
16-
```
17-
$ sudo apt-get install python3 python3-pip ninja-build
18-
```
19-
20-
## How to install
21-
```
22-
$ cd <library_directory>
23-
$ mkdir build
24-
$ meson build && cd build
25-
$ ninja
26-
$ sudo ninja install
27-
```
28-
29-
If you have problems, that library can't be found run `sudo ldconfig /usr/local/lib/x86_64-linux-gnu/` if you haven't changed prefix, else mofidy path.
30-
31-
## How to include library to your c or c++ project ?
32-
It's simple. At the top of your source file include library:
33-
``` c++
34-
#include<ByteConvert/ByteConvert.hpp>
35-
```
3617

37-
And when you compile add flag `-lbyteconvert`, example `g++ -std=c++11 MyProject.cpp -lbyteconvert -o MyProject`.
38-
## Examples
39-
Convert numeric variable for eg. `int`,`short`,`float`,`double` to array of bytes.
40-
``` c++
41-
int somevar = 5;
42-
size_t blk_size = 0;
43-
uint8_t *block = ByteConvert::varToArray<int>(blk_size,somevar);
44-
45-
// Use array
18+
You need [python3](https://www.python.org/), [ninja](https://ninja-build.org/) and [meson](http://mesonbuild.com).
4619

47-
delete block; // Don't forget to free memory, when you don't need array any more
20+
```bash
21+
sudo apt-get install python3 python3-pip ninja-build
22+
sudo pip3 install meson
4823
```
4924

50-
Convert array of bytes to numeric variable.
51-
``` c++
52-
uint8_t *block; // Predefined byte array with size of int
53-
int somevar = ByteConvert::arrayToVar<int>(block);
54-
55-
// Use block & somevar
25+
## Installing
26+
```bash
27+
meson build
28+
ninja -C build
29+
ninja -C build test
30+
ninja -C build install
5631

57-
delete block; // Don't forget to free memory, when you don't need array any more
58-
59-
// Use somevar
32+
# Or simply
33+
make
34+
sudo make install
6035
```
6136

62-
Convert array of bytes to string of hex characters
63-
``` c++
64-
size_t blk_size; // Predefined size of byte array
65-
uint8_t *block; // Predefined byte array with size of int
66-
String somevar = ByteConvert::arrayToString(blk_size,block);
37+
If you have problems, that library can't be found run `sudo ldconfig /usr/local/lib/x86_64-linux-gnu/` if you haven't changed prefix, else modify path.
6738

68-
// Use block & somevar
39+
## How to include library to your c or c++ project ?
6940

70-
delete block; // Don't forget to free memory, when you don't need array any more
41+
It's simple. At the top of your source file include library:
7142

72-
// Use somevar
43+
```c++
44+
#include <ByteConvert/ByteConvert.hpp>
7345
```
7446

75-
Convert string of hex characters to array of bytes
76-
``` c++
77-
String somevar = ""; // Predefined string
78-
size_t blk_size = 0;
79-
uint8_t *block = ByteConvert::stringToArray(blk_size,somevar);
47+
And when you compile add flag `-lbyteconvert`, example `g++ -std=c++11 MyProject.cpp -lbyteconvert -o MyProject`.
8048

81-
// Use block
49+
## Examples
8250

83-
delete block; // Don't forget to free memory, when you don't need array any more
84-
```
51+
See [**examples folder**](https://github.com/SloCompTech/ByteConvert_cpp/tree/master/examples)

examples/SimpleTest.cpp

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)