meal_pager_calc.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "meal_pager_calc.h"
  2. void customConcat(char* dest, const char* src) {
  3. // Find the end of the destination string
  4. while(*dest != '\0') {
  5. dest++;
  6. }
  7. // Copy characters from src to dest
  8. while(*src != '\0') {
  9. *dest = *src;
  10. dest++;
  11. src++;
  12. }
  13. // Null-terminate the concatenated string
  14. *dest = '\0';
  15. }
  16. char* encManchester(const char* bits, int mode) {
  17. // Allocate memory for the result string
  18. char* res = (char*)malloc((strlen(bits) * 2 + 1) * sizeof(char));
  19. int index = 0;
  20. for(int i = 0; bits[i] != '\0'; i++) {
  21. char c = bits[i];
  22. if(c == '0') {
  23. if(mode) {
  24. res[index++] = '1';
  25. res[index++] = '0';
  26. } else {
  27. res[index++] = '0';
  28. res[index++] = '1';
  29. }
  30. } else if(c == '1') {
  31. if(mode) {
  32. res[index++] = '0';
  33. res[index++] = '1';
  34. } else {
  35. res[index++] = '1';
  36. res[index++] = '0';
  37. }
  38. } else {
  39. // Handle 'EE' case (error)
  40. res[index++] = 'E';
  41. res[index++] = 'E';
  42. }
  43. }
  44. // Null-terminate the result string
  45. res[index] = '\0';
  46. return res;
  47. }
  48. void uint32ToBinaray(uint32_t number, char* str, int8_t length) {
  49. int i = 0;
  50. length--; // count length without 0
  51. for(i = length; i >= 0; i--) {
  52. // Bitwise AND extration of the i-th bit
  53. int bit = (number >> i) & 1;
  54. // convert the bit to a character of 1 or 0
  55. str[length - i] = bit + '0';
  56. }
  57. // Terminate the string
  58. str[length + 1] = '\0';
  59. }
  60. void reverse(char* str) {
  61. int length = strlen(str);
  62. int start = 0;
  63. int end = length - 1;
  64. while(start < end) {
  65. char temp = str[start];
  66. str[start] = str[end];
  67. str[end] = temp;
  68. start++;
  69. end--;
  70. }
  71. }