Skip to content

Instantly share code, notes, and snippets.

@AndreasLonn
Last active November 25, 2021 11:38
Show Gist options
  • Save AndreasLonn/cbd6083e7b1303b6907cf9a232dc17a7 to your computer and use it in GitHub Desktop.
Save AndreasLonn/cbd6083e7b1303b6907cf9a232dc17a7 to your computer and use it in GitHub Desktop.
Add these files to your project directory. Then use the header file 'hjalpfunktioner.h' (#include "hjalpfunktioner.h") to use the functions in your program
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/**
* @brief Converts an integer to a string in base 2 (binary)
*
* @param num Integer to convert to string
* @param format (Default = true) Whether or not to divide into groups of 8 and add zeros to the first group
* @return A string of ones, zeros and (if format) spaces
*/
string bitar(const unsigned int num, const bool format = true) {
string r = "";
int8_t i = log2(num);
if(format) for(uint8_t b = 7 - i % 8; b > 0; b--) r += "0";
while(i >= 0) {
if (format && !((i+1) % 8) && r.length()) r += " ";
r += 0x30 + (num >> i--) % 2;
}
return r;
}
/**
* @brief Converts an array of integers to a string separated by the specified string
*
* @param array Array of integers to list
* @param length Length of the array
* @param sep (Default = ", ") String to place between the numbers in the array
* @return A string of integers spearated by the specified string
*/
string intArray(const int array[], const unsigned int length, const string sep = ", ") {
string r = "";
for(unsigned int i = 0; i < length; i++)
r += (i > 0 ? sep : "") + to_string(array[i]);
return r;
}
/**
* @brief Converts an array of 8-bit unsigned integers to a string separated by the specified string
*
* @param array Array of 8-bit unsigned integers to list
* @param length Length of the array
* @param sep (Default = ", ") String to place between the numbers in the array
* @return A string of integers spearated by the specified string
*/
string intArray(const uint8_t array[], const unsigned int length, const string sep = ", ") {
string r = "";
for(unsigned int i = 0; i < length; i++)
r += (i > 0 ? sep : "") + to_string(array[i]);
return r;
}
/**
* @brief Copies the elements of the source array into the destination array
*
* @param source Source array (integer array)
* @param dest Destination array (integer array)
* @param length Length of the array
*/
void copyArray(const int source[], int dest[], const unsigned int length) {
for(unsigned int i = 0; i < length; i++)
dest[i] = source[i];
}
/**
* @brief Copies the elements of the source array into the destination array
*
* @param source Source array (8-bit unsigned integer array)
* @param dest Destination array (8-bit unsigned integer array)
* @param length Length of the array
*/
void copyArray(const uint8_t source[], uint8_t dest[], const unsigned int length) {
for(unsigned int i = 0; i < length; i++)
dest[i] = source[i];
}
/**
* @brief Returns the sum of the elements in the array
*
* @param array Array of integers
* @param length Length of the array
* @return The sum of the elements
*/
int sumArray(const int array[], const unsigned int length) {
int r = 0;
for(unsigned int i = 0; i < length; i++)
r += array[i];
return r;
}
/**
* @brief Returns the sum of the elements in the array
*
* @param array Array of 8-bit unsigned integers
* @param length Length of the array
* @return The sum of the elements
*/
unsigned int sumArray(const uint8_t array[], const unsigned int length) {
unsigned int r = 0;
for(unsigned int i = 0; i < length; i++)
r += array[i];
return r;
}
#include <string>
std::string bitar(const unsigned int tal, const bool format);
std::string intArray(const int array[], const unsigned int length, const std::string sep = ", ");
std::string intArray(const uint8_t array[], const unsigned int length, const std::string sep = ", ");
void copyArray(const int source[], int dest[], const unsigned int length);
void copyArray(const uint8_t source[], uint8_t dest[], const unsigned int length);
unsigned int sumArray(const int array[], const unsigned int length);
unsigned int sumArray(const uint8_t array[], const unsigned int length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment