Skip to content

Commit 4f1aea5

Browse files
committed
Merge branch 'release/0.1.0'
2 parents 6807ec7 + 1cfa6da commit 4f1aea5

File tree

6 files changed

+82
-12
lines changed

6 files changed

+82
-12
lines changed

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: c
2+
sudo: false
3+
before_install:
4+
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
5+
install:
6+
- arduino --install-library "ByteConvert"
7+
script:
8+
- build_main_platforms
9+
notifications:
10+
email:
11+
on_success: change
12+
on_failure: change

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1-
ByteConvert
1+
# ByteConvert
2+
[![Build Status](https://travis-ci.org/SloCompTech/ByteConvert_arduino.svg?branch=master)](https://travis-ci.org/SloCompTech/ByteConvert_arduino)
3+
[![Packagist](https://img.shields.io/packagist/l/doctrine/orm.svg)]()
4+
5+
## What's about ?
6+
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.
7+
8+
## What you need to consider, when you are using this library
9+
When you are using this library, you need to consider variable byte size, because if you are using different platform, then there may be some errors, because int on platform 1 has 4 bytes and int on platform 2 has 2 bytes.
10+
11+
## Examples
12+
Convert numeric variable for eg. `int`,`short`,`float`,`double` to array of bytes.
13+
``` c++
14+
int somevar = 5;
15+
size_t blk_size = 0;
16+
uint8_t *block = ByteConvert::varToArray<int>(blk_size,somevar);
17+
18+
// Use array
19+
20+
delete block; // Don't forget to free memory, when you don't need array any more
21+
```
22+
23+
Convert array of bytes to numeric variable.
24+
``` c++
25+
uint8_t *block; // Predefined byte array with size of int
26+
int somevar = ByteConvert::arrayToVar<int>(block);
27+
28+
// Use block & somevar
29+
30+
delete block; // Don't forget to free memory, when you don't need array any more
31+
32+
// Use somevar
33+
```
34+
35+
Convert array of bytes to string of hex characters
36+
``` c++
37+
size_t blk_size; // Predefined size of byte array
38+
uint8_t *block; // Predefined byte array with size of int
39+
String somevar = ByteConvert::arrayToString(blk_size,block);
40+
41+
// Use block & somevar
42+
43+
delete block; // Don't forget to free memory, when you don't need array any more
44+
45+
// Use somevar
46+
```
47+
48+
Convert string of hex characters to array of bytes
49+
``` c++
50+
String somevar = ""; // Predefined string
51+
size_t blk_size = 0;
52+
uint8_t *block ByteConvert::stringToArray(blk_size,somevar);
53+
54+
// Use block
55+
56+
delete block; // Don't forget to free memory, when you don't need array any more
57+
```

examples/SimpleTest/SimpleTest.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ void setup() {
1313
Serial.println("byteToHexString: fa->"+String(ByteConvert::byteToHexString(0xfa)));
1414

1515
uint8_t array1[] = {0xfa,0xca,0xde,0xda};
16-
Serial.println("facadeda -> "+ByteConvert::arrayToString(array1,4));
16+
Serial.println("facadeda -> "+ByteConvert::arrayToString(4,array1));
1717

1818
size_t s2 = 0;
1919
uint8_t *array2 = ByteConvert::stringToArray(s2,"bedababa");
2020
Serial.println("Size(bedababba): "+String(s2));
21-
Serial.println("bedababba -> "+ ByteConvert::arrayToString(array2,s2));
21+
Serial.println("bedababba -> "+ ByteConvert::arrayToString(s2,array2));
2222

2323
delete array2;
2424

2525
int in = 5;
2626
Serial.println("Orig: 5 -> "+String(in));
2727
size_t s3 = 0;
2828
uint8_t *array3 = ByteConvert::varToArray<int>(s3,in);
29-
Serial.println("? -> "+ ByteConvert::arrayToString(array3,s3));
29+
Serial.println("0005 -> "+ ByteConvert::arrayToString(s3,array3));
3030
Serial.println("5 -> "+ String(ByteConvert::arrayToVar<int>(array3)));
3131

3232
delete array3;

library.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=ByteConvert
2-
version=0.0.1
3-
author=SloCompTech
4-
maintainer=SloCompTech <[email protected]>
2+
version=0.1.0
3+
author=Martin Dagarin
4+
maintainer=Martin Dagarin <[email protected]>
55
sentence=Library for converting variables to bytes and reverse
66
paragraph=Library for converting variables to bytes so they are easyer to transmit via I2C,SPI ...
77
category=Communication
8-
url=None
8+
url=https://github.com/SloCompTech/ByteConvert_arduino
99
architectures=*
1010
includes=ByteConvert.hpp

src/ByteConvert.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ namespace ByteConvert {
2525
return 16*hexToVal(block.charAt(0))+hexToVal(block.charAt(1));
2626
}
2727

28-
String arrayToString(uint8_t *src,size_t size) {
28+
String arrayToString(size_t size,uint8_t *src) {
2929
String buffer = "";
3030
for (size_t i = 0;i < (size);i++)
3131
buffer += byteToHexString(src[i]);
3232
return buffer;
3333
}
3434
uint8_t* stringToArray(size_t &size,String src) {
35+
// Length of src must be odd !!
36+
if (src.length() % 2 == 1)
37+
src = "0" + src;
3538
size = src.length()/2;
3639
uint8_t *dst = new uint8_t[size]; // Allocate memory space
3740
for (size_t i = 0;i < (size);i++)
3841
dst[i] = hexStringToByte(String(src.charAt(i*2))+String(src.charAt(i*2+1)));
39-
Serial.println(arrayToString(dst,size));
4042
return dst;
4143
}
4244
}

src/ByteConvert.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ByteConvert {
1717
template<class T>
1818
uint8_t *varToArray(size_t &size,T var) {
1919
size = sizeof(T)/sizeof(uint8_t);
20-
uint8_t *array = new uint8_t[size];
20+
uint8_t *array = new uint8_t[size]; // Alocate memory
2121
for (size_t i = 0;i < size;i++) {
2222
if (i != 0) var >>= 8;
2323
array[size-1-i] = (uint8_t)(var&0xff);
@@ -28,7 +28,7 @@ namespace ByteConvert {
2828
uint8_t hexToVal(char c);
2929
String byteToHexString(uint8_t b);
3030
uint8_t hexStringToByte(String block);
31-
String arrayToString(uint8_t *src,size_t size);
31+
String arrayToString(size_t size,uint8_t *src);
3232
uint8_t* stringToArray(size_t &size,String src);
3333
}
3434

0 commit comments

Comments
 (0)