I have try your this code and its working, but its not exactly what i need, i have try to make it work for my need with no luck..
What i actually need in a function to convert a string (that contain HEX) and result as string...
Here is what i need:
1 = my source is a STRING that contain HEX, not a char array.
2 = my source can have different length
3 = i want the result to be inside a string
Exemple:
//Exemple of the function that i need
String HEXstring = "74657374"; // This is my HEX string, can be any hex of any size
String ASCIIValue = ""; // Used to put the result
// I want to use it that way:
ASCIIValue = ConvertToASCII(HEXstring);
// Now ASCIIValue contain "TEST" (because my hex is "74657374")
Can you help me to build this "ConvertToASCII" function?
econjack:
OP: You are aware of the resource costs of using the String class instead of a char array, right?
Well i'm using a gsm module and the output received from the serial port is something like: "74657374", so i want to compare this data with another string..
exemple:
if (receiveddata = "test") .....
So to do that i need to "change" the received data into an ascii string (in this case "74657374" changed to "TEST")
I DONT WANT to compare with hex, exemple: if (receiveddata = "74657374") .....
So thats why i need a function to "convert" my string(that contain hex number) into another string (witch contain the ASCII value of the first string number)
for sure i can write a function like :
...
if (receiveddata = "74"){ return "T";}
if (receiveddata = "75"){ return "U";}
etc...
...
But there is probably a better way to do what i want?
Show your whole code or at least a full example. I doubt you are receiving a hex string from your gsm module... more likely a serie of bytes that YOU convert to a hex string.
Forget about the String class, use C string instead (char arrays).
guix:
Show your whole code or at least a full example. I doubt you are receiving a hex string from your gsm module... more likely a serie of bytes that YOU convert to a hex string.
Forget about the String class, use C string instead (char arrays).
Are you sure the GSM is sending an ASCII '4' and ASCII '8' and not the single character 48? That would be really weird. As said before, show the code that receives this string from your GSM.
KeithRB:
Are you sure the GSM is sending an ASCII '4' and ASCII '8' and not the single character 48? That would be really weird. As said before, show the code that receives this string from your GSM.
void setup() {
char input[] = "48454C4C4F"; // Should translate to HELLO
char temp[3];
char c;
int index;
int i;
int val;
Serial.begin(9600);
for (i = 0; i < sizeof(input) - 1; i += 2) {
temp[0] = input[i];
temp[1] = input[i + 1];
val = ASCIIHexToInt(temp[0]) * 16; // First Hex digit
val += ASCIIHexToInt(temp[1]); // Second hex digit
c = toascii(val);
Serial.print(c);
}
}
int ASCIIHexToInt(char c)
{
int ret = 0;
if ((c >= '0') && (c <= '9'))
ret = (ret << 4) + c - '0';
else
ret = (ret << 4) + toupper(c) - 'A' + 10;
return ret;
}
void loop() {
}
econjack:
Take Ray's code and modify it a little to get:
void setup() {
char input[] = "48454C4C4F"; // Should translate to HELLO
char temp[3];
char c;
int index;
int i;
int val;
Serial.begin(9600);
for (i = 0; i < sizeof(input) - 1; i += 2) {
temp[0] = input[i];
temp[1] = input[i + 1];
val = ASCIIHexToInt(temp[0]) * 16; // First Hex digit
val += ASCIIHexToInt(temp[1]); // Second hex digit
c = toascii(val);
Serial.print(c);
}
}
int ASCIIHexToInt(char c)
{
int ret = 0;
if ((c >= '0') && (c <= '9'))
ret = (ret << 4) + c - '0';
else
ret = (ret << 4) + toupper(c) - 'A' + 10;
return ret;
}
void loop() {
}
This code is not doing what i want: it take a CHAR as input and the output is a CHAR too.. As i already said before: I need something that convert from a STRING to a STRING.