├── Convert.cpp ├── Convert.h ├── LICENSE.md ├── README.md ├── examples └── data_convertion_example.ino └── keywords.txt /Convert.cpp: -------------------------------------------------------------------------------- 1 | #include "Arduino.h" 2 | #include "Convert.h" 3 | 4 | Convert::Convert(){} 5 | 6 | /** 7 | 1: Convert the string into char array 8 | 2: Use char array with cpp strtol and base 9 | 10 | Reference: http://www.cplusplus.com/reference/cstdlib/strtol/ 11 | */ 12 | long Convert::binaryToDecimal(String value){ 13 | int base = 2; 14 | int length = value.length() +1; 15 | char valueAsArray[length]; 16 | value.toCharArray(valueAsArray, length); 17 | 18 | return strtol(valueAsArray, NULL, base); 19 | } 20 | 21 | /** 22 | 1: Convert the string into char array 23 | 2: Use char array with cpp strtol and base 24 | 25 | Reference: http://www.cplusplus.com/reference/cstdlib/strtol/ 26 | */ 27 | long Convert::hexaToDecimal(String value){ 28 | int base = 16; 29 | int length = value.length() +1; 30 | char valueAsArray[length]; 31 | value.toCharArray(valueAsArray, length); 32 | return strtol(valueAsArray, NULL, base); 33 | } 34 | 35 | /** 36 | 1: Convert the string into char array 37 | 2: Use char array with cpp strtol and base 38 | 39 | Reference: http://www.cplusplus.com/reference/cstdlib/strtol/ 40 | */ 41 | long Convert::octalToDecimal(String value){ 42 | int base = 8; 43 | int length = value.length() +1; 44 | char valueAsArray[length]; 45 | value.toCharArray(valueAsArray, length); 46 | return strtol(valueAsArray, NULL, base); 47 | } 48 | 49 | /** 50 | Using the divide by 2 and remainder method to create a binary string 51 | 52 | Reference: http://www.wikihow.com/Convert-from-Decimal-to-Binary 53 | */ 54 | String Convert::decimalToBinary(long value){ 55 | String result = ""; 56 | if(value == 0){ 57 | return "0"; 58 | }else{ 59 | while(value > 0){ 60 | if((value % 2) == 0){ 61 | result = "0" + result; 62 | }else{ 63 | result = "1" + result; 64 | } 65 | value /= 2; 66 | } 67 | } 68 | return result; 69 | } 70 | 71 | 72 | /** 73 | 1: Convert the decimal value into a binary String 74 | 2: Each 4 bit pattern is translated into hexadecimal 75 | 76 | Reference: http://www.rapidtables.com/convert/number/how-binary-to-hex.htm 77 | */ 78 | String Convert::decimalToHexa(long value){ 79 | String valueAsBinary = decimalToBinary(value); 80 | String valueAsHexa = ""; 81 | String tmp = ""; 82 | 83 | // Loop over the binary list from right to left 84 | for(int i = valueAsBinary.length() ; i > 0; i--){ 85 | if(tmp.length() == 4){ 86 | valueAsHexa = fourBitsToHex(tmp) + valueAsHexa; 87 | tmp = ""; 88 | } 89 | tmp = valueAsBinary.substring(i, i-1) + tmp; 90 | } 91 | 92 | valueAsHexa = fourBitsToHex(tmp) + valueAsHexa; 93 | return valueAsHexa; 94 | } 95 | 96 | /** 97 | 1: Convert the decimal value into a binary String 98 | 2: Each 3 bit pattern is translated into octal 99 | 100 | Reference: http://www.wikihow.com/Convert-Binary-to-Octal-Number 101 | */ 102 | String Convert::decimalToOctal(long value){ 103 | String valueAsBinary = decimalToBinary(value); 104 | String valueAsOctal = ""; 105 | String tmp = ""; 106 | 107 | // Loop over the binary list from right to left 108 | for(int i = valueAsBinary.length() ; i > 0; i--){ 109 | if(tmp.length() == 3){ 110 | valueAsOctal = String(binaryToDecimal(tmp)) + valueAsOctal; 111 | tmp = ""; 112 | } 113 | tmp = valueAsBinary.substring(i, i-1) + tmp; 114 | } 115 | 116 | valueAsOctal = String(binaryToDecimal(tmp)) + valueAsOctal; 117 | return valueAsOctal; 118 | 119 | } 120 | 121 | /** 122 | This method will translate a 4 bit binary value into the correct hexadecimal didgit 123 | */ 124 | String Convert::fourBitsToHex(String value){ 125 | int decValue = binaryToDecimal(value); 126 | switch(decValue){ 127 | case 10: 128 | return "A"; 129 | break; 130 | case 11: 131 | return "B"; 132 | break; 133 | case 12: 134 | return "C"; 135 | break; 136 | case 13: 137 | return "D"; 138 | break; 139 | case 14: 140 | return "E"; 141 | break; 142 | case 15: 143 | return "F"; 144 | break; 145 | default: 146 | return String(decValue); 147 | break; 148 | } 149 | } -------------------------------------------------------------------------------- /Convert.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef Convert_h 3 | #define Convert_h 4 | 5 | #include "Arduino.h" 6 | 7 | class Convert 8 | { 9 | public: 10 | Convert(); 11 | long binaryToDecimal(String value); 12 | long hexaToDecimal(String value); 13 | long octalToDecimal(String value); 14 | String decimalToBinary(long value); 15 | String decimalToHexa(long value); 16 | String decimalToOctal(long value); 17 | private: 18 | String fourBitsToHex(String value); 19 | }; 20 | 21 | #endif -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Carl Ohlsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino Converter 2 | ----------------- 3 | 4 | This is a library for converting integer / long numbers in decimal form to other bases like Hexadecimal, Binary and Octal. 5 | It could also be used to generate an integer / long from a string containing a Hexadecimal, Binary or Octal numbers. 6 | 7 | 8 | Currently Supporting 9 | -------------------- 10 | Decimal -> Binary 11 | Decimal -> Hexadecimal 12 | Decimal -> Octal 13 | 14 | Binary -> Decimal 15 | Hexadecimal -> Decimal 16 | Octal -> Decimal 17 | 18 | 19 | -------------------------------------------------------------------------------- /examples/data_convertion_example.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Creation of class object 4 | Convert convert; 5 | 6 | void setup() { 7 | Serial.begin(9600); 8 | int dec = 50; 9 | //Conversion de decimal a binario 10 | Serial.print("50 converted to binary: "); 11 | Serial.println(convert.decimalToBinary(dec)); 12 | //Conversion de decimal a hexadecimal 13 | Serial.print("50 converted to hexadecimal: "); 14 | Serial.println(convert.decimalToHexa(dec)); 15 | //Conversion de decimal a octal 16 | Serial.print("50 converted to octal: "); 17 | Serial.println(convert.decimalToOctal(dec)); 18 | 19 | String hex = "aabbab"; 20 | //Conversion from hexadecimal to decimal 21 | Serial.print("AABBAB converted to decimal: "); 22 | Serial.println(convert.hexaToDecimal(hex)); 23 | 24 | 25 | String bin = "11001111"; 26 | //Conversion from binary to decimal 27 | Serial.print("11001111 converted to decimal: "); 28 | Serial.println(convert.binaryToDecimal(bin)); 29 | 30 | String oct = "515"; 31 | //Conversion from octal to decimal 32 | Serial.print("515 converted to decimal: "); 33 | Serial.println(convert.octalToDecimal(oct)); 34 | } 35 | 36 | void loop() { 37 | //do nothing 38 | } 39 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | Convert KEYWORD1 2 | binaryToDecimal KEYWORD2 3 | hexaToDecimal KEYWORD2 4 | octalToDecimal KEYWORD2 5 | decimalToBinary KEYWORD2 6 | decimalToHexa KEYWORD2 7 | decimalToOctal KEYWORD2 8 | fourBitsToHex KEYWORD2 9 | --------------------------------------------------------------------------------