The problem is that WORD is not a variable so you can't use the usual syntax? like str[i] So WORD[] is not accepted? Or only not understand that 'A' char is ascii 65 (41hex) 'T' = 84 (54hex) and you send 65, after 84 (you see dec, you can think in hex, but microcontroller see only binary? * *... MySend(WORD); // c compiler see MySend("textToConvert"); ... void MySend(char *pWord) { for(int i=0;i<=strlen(pWORD);i++) { obj.SendByte(*(pWord+i)); // I don't know command for send byte to xbee but I think you can send byte } }* *
What I don't know, is how to convert a number in hex to str.
You don't have "a number in hex". You have a number. If you want a string representation of that number in hex format, google sprintf() and the %x format specifier.
I have to send a string with hexadecimal representation.
I have a char* with my pass, then I need to convert to another char* containing hexadecimal representation of that string, and then, send it to my xbee.
So, I need to convert this: #define WORD "password" to other char* containing 70617373776F7264.
But password can change, so I need a function to convert it.
#include <stdio.h> // for sprintf
...
MySend(WORD); // c compiler see MySend("textToConvert");
...
void MySend(char *pWord)
{ char buf[2];
for(int i=0;i<=strlen(pWORD);i++)
{ char c=*(pWord+i); // single ascii value
sprintf(buf,"%x",c);// convert ascii value in hex
obj.SendByte(buf[0]); // I don't know command for send byte to xbee but I think you can send byte
obj.SendByte(buf[1]);
}
}
But password can change, so I need a function to convert it.
Then forget the #define idea. Write a function.
Show some code. Why does the mysterious device expect your password in hex form?
With "can change" I mean that next time I upload pde, I can change the password in define. No during execution. It is not a mysterious device, it is a XBee. To configure XBee by AT commands (ie pass) you have to send XBee.println(ATKY70617373776F7264);, no XBee.println(ATKYWORD);
Thirteen posts and it still isn't clear what you are actually trying to do.
If you have a 'T' character in your original character array, that is represented by a char or a byte
( on the arduino, the same thing ) with 01010100 in it. This also happens to be the number 84
represented in binary, or it can be represented by 0101 0100 as 2 hexadecimal digits 54 in hex.
If you want to print the hex digits 5 and 4, you are then going to print 2 characters, '5' and '4'.
If you want to put these characters into a character array for some reason, you will need space
for two characters plus another byte for the character array terminating zero byte ( unless you know
how to work around that, which you obviously don't ).
if you actually are sending Xbee commands in the format you speficied in your last comment-
XBee.println(ATKYWORD);
Then that's wrong. I can't speak from personal experience as I have never used an XBee, but I've used a couple of other arduino shields that recieve AT commands (3G shield, cell shield) and I know that those commands have to be enclosed in quotes within the serial.println command
i.e.
nid69ita your code works well. Just have some fails if you use number instead characters for password, something like this: "password11". I will try to solve it. Thank you.
I wrote XBee.println(ATKYWORD);, I already know it fails because it has not enclosure for ATKYWORD.
I have no problems to write password in define in hexadecimal but my program will be use by people who doesn't know what hexadecimal is, so I cannot do that. They can be able to write a word as their password and after that, program will convert it.
I have all my commands in defines so ATKY represents "atky" actually. WORD will be the password... I just meaned the way commands are sent to XBee.
I didn't post code just because I didn't have any code for that. I was trying to convert the sentence to start to do a function for that but I haven't the program yet.
Problem can be implicit conversion made by compiler.
Try with password with also number, like ABC123
Then add a simple Serial.println(c);
#include <stdio.h> // for sprintf
...
MySend(WORD); // c compiler see MySend("textToConvert");
...
void MySend(char *pWord)
{ char buf[2];
for(int i=0;i<=strlen(pWORD);i++)
{ char c=*(pWord+i); // single ascii value
Serial.println(c);
sprintf(buf,"%0x",c);// convert ascii value in hex
obj.SendByte(buf[0]); // I don't know command for send byte to xbee but I think you can send byte
obj.SendByte(buf[1]);
}
}
If you see problems when show one numeric char, try using unsigned char or unsigned byte or also unsigned int.
Sample on internet about sprintf use only integer value.