fork download
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4.   GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
  5.   C#, OCaml, VB, Perl, Swift, Prolog, Javascript, Pascal, COBOL, HTML, CSS, JS
  6.   Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <inttypes.h>
  13.  
  14. uint16_t* manchester_encoder(char* string, size_t length) {
  15. uint16_t* encoded = malloc(length * 2 * sizeof(uint16_t));
  16. uint16_t* encoded_ptr = encoded;
  17. uint8_t bit=0;
  18. for(size_t x = 0; x < length; x++,encoded++) {
  19. for(int8_t y=7; y>=0; y--) {
  20. bit=0;
  21. if(y%2!=0) { // Impares
  22. bit=(string[x] & (1 << y));
  23. if(bit==0) {
  24. *encoded = *encoded & (0<<((y*2)+1));
  25. *encoded = *encoded | (1<<(y*2));
  26. } else {
  27. *encoded = *encoded | (1<<((y*2)+1));
  28. *encoded = *encoded & (0<<(y*2));
  29. }
  30. } else { // Pares
  31. bit=(string[x] & (1 << y));
  32. if(bit==0) {
  33. *encoded = *encoded & (0<<y);
  34. *encoded = *encoded | (1<<(y+1));
  35. } else {
  36. *encoded = *encoded | (1<<y);
  37. *encoded = *encoded & (0<<(y+1));
  38. }
  39. }
  40. }
  41. }
  42. return encoded_ptr;
  43. }
  44.  
  45. char* manchester_decoder(uint16_t* manchester, size_t length) {
  46. char* decoded = malloc(length / 2 * sizeof(uint8_t));
  47. char* decoded_ptr = decoded;
  48. for(size_t x = 0; x < length / 2; x++) {
  49. uint8_t bit = 0;
  50. for(int8_t y = 7; y >= 0; y--) {
  51.  
  52. if(y%2!=0) // Impares
  53. bit= (manchester[x] & (1 << (y * 2 + 1)));
  54.  
  55. else // Pares
  56. bit = (manchester[x] & (1 << y));
  57.  
  58. if(bit==0)
  59. *decoded = *decoded & (0 << (y));
  60. else
  61. *decoded = *decoded | (1 << (y));
  62. }
  63. }
  64. return decoded_ptr;
  65. }
  66.  
  67. int main()
  68. {
  69. char *mensaje = "PRUEBA";
  70. uint16_t *encoded = NULL;
  71. char *decoded = NULL;
  72.  
  73. printf("%s",mensaje);
  74.  
  75. encoded=manchester_encoder(mensaje,strlen(mensaje));
  76.  
  77. printf("\nEncoded: \n");
  78.  
  79. for(size_t x=0; x<=strlen(mensaje)*2; x++) {
  80. printf("%04x",encoded);
  81. }
  82.  
  83. printf("\nDecoded: \n");
  84.  
  85. decoded=manchester_decoder(encoded,strlen(mensaje)*2);
  86.  
  87. printf("%s",decoded);
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0.01s 5316KB
stdin
45
stdout
PRUEBA
Encoded: 
ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270ab631270
Decoded: