├── LICENSE.md ├── README.md └── hex_dec.ino /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) [year] [fullname] 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino-Hex-Decimal-Conversion 2 | ============================== 3 | 4 | Utility functions for converting values between hex strings and decimal numbers on Arduino. (Helpful for color conversion). 5 | 6 | Licensed for any use under the MIT License. -------------------------------------------------------------------------------- /hex_dec.ino: -------------------------------------------------------------------------------- 1 | 2 | // Converting from Hex to Decimal: 3 | 4 | // NOTE: This function can handle a positive hex value from 0 - 65,535 (a four digit hex string). 5 | // For larger/longer values, change "unsigned int" to "long" in both places. 6 | 7 | 8 | unsigned int hexToDec(String hexString) { 9 | 10 | unsigned int decValue = 0; 11 | int nextInt; 12 | 13 | for (int i = 0; i < hexString.length(); i++) { 14 | 15 | nextInt = int(hexString.charAt(i)); 16 | if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9); 17 | if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15); 18 | if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15); 19 | nextInt = constrain(nextInt, 0, 15); 20 | 21 | decValue = (decValue * 16) + nextInt; 22 | } 23 | 24 | return decValue; 25 | } 26 | 27 | 28 | 29 | 30 | 31 | // Converting from Decimal to Hex: 32 | 33 | // NOTE: This function can handle a positive decimal value from 0 - 255, and it will pad it 34 | // with 0's (on the left) if it is less than the desired string length. 35 | // For larger/longer values, change "byte" to "unsigned int" or "long" for the decValue parameter. 36 | 37 | 38 | String decToHex(byte decValue, byte desiredStringLength) { 39 | 40 | String hexString = String(decValue, HEX); 41 | while (hexString.length() < desiredStringLength) hexString = "0" + hexString; 42 | 43 | return hexString; 44 | } --------------------------------------------------------------------------------