Posted By

jatkins on 07/10/09


Tagged

hex generator ascii conversion Converter convert hexadecimal arduino octet octets


Versions (?)

ARDUINO: ASCII to OCTET converter


 / Published in: Processing
 

BUG: The Arduino language (a C derivative based on Processing) suppresses leading zeros when rendering hexadecimal data, therefore the resultant octets may be corrupted. A fix can be found at http://www.phanderson.com/arduino/arduino_display.html (scroll to the 'print_hex' function), but has not been included due to copyright concerns.

  1. /*Test: void setup() {
  2.   Serial.begin(9600); // initialize serial port
  3. }
  4.  
  5. void loop() {
  6.   delay(1000);
  7.   Serial.println("AT+CMGF=0"); // set to PDU mode
  8.   delay(1000);
  9.   Serial.print("AT+CMGS=");
  10.   delay(1000);
  11.   Serial.println("41");
  12.   delay(1000);
  13.   Serial.print("0001000B817079279626F500001FE8329BFD4697D9EC37089D06CDCB707AB92D2ECB41B21B480683E500");
  14.   delay(1000);
  15.   Serial.println(0x1A, BYTE);
  16.   delay(5000);
  17.  }*/
  18.  
  19. int i, m, y, n, sms_len, octets, interval = 1000;
  20. char msg[] = "hellohello";
  21. byte byteLow;
  22.  
  23. void setup() {
  24. Serial.begin(9600); // activate UART at 9600 bps
  25.  
  26. for(i=0;i<sizeof(msg)-1;i++) {
  27. msg[i] >>= n; // shift msg[i] to right by n, to make space for rightmost part of next byte
  28. if(n==7) { // end of byte, rollover
  29. n = -1;
  30. }
  31. else {
  32. if(i<sizeof(msg)-2) {
  33. m = n;
  34. y = 7;
  35. while(m>-1) {
  36. bitWrite(msg[i], y, bitRead(msg[i+1], m)); // insert rightmost part of next byte (msg[i+1]) to left of msg[i]
  37. m--;
  38. y--;
  39. }
  40. if(lowByte(msg[i])) { // increment octet count
  41. octets++;
  42. }
  43. }
  44. }
  45. n++;
  46. }
  47.  
  48. byteLow = lowByte(msg[i]);
  49.  
  50. for(i=0;i<sizeof(msg)-1;i++) {
  51. if(lowByte(msg[i])) {
  52. byteLow = lowByte(msg[i]);
  53. Serial.print(byteLow, HEX); // print octet
  54. }
  55. }
  56. }
  57.  
  58. void loop() {
  59. }

Report this snippet  

You need to login to post a comment.