Im trying to convert a string like hello world to hex and get the amount of bytes but am not getting it to work
I am making a script that gets the sensor value and converts it to hex, gets the amount of bytes, and then sends it via nb-iot.
AT+NSOST=0,"172.27.131.100",15683,11,"48656c6c6f20576f726c64"
(11 is referring to the amount of bytes and the string stands for 'Hello World' in hex.)
Im trying to convert a string like hello world to hex
That makes NO sense. Hex is one way of showing a numeric value. The value 10, in a base 10 system would be represented as 0xA in Hex.
Does anyone know how I can fix this?
I can't even understand what is broken. What you are doing, even if you were doing it properly, makes not sense. And, you are NOT doing ir properly.
Serial.print("%x" + str[i]);
Adding a string and a character does not make sense. What does your calculator show when you try to add %x and h? How do you even enter those into the calculator?
The + operator does addition. Nothing else, unless there are class instances involved, and the class overloads the + operator.
In your case, there are no class instances involved, so no class overloaded the + operator.
convert a string like hello world to hex and get the amount of bytes
The amount of bytes in a string is just the number of characters, strlen() will give you that number
If you would wantto know the sum of all bytes in the string, do sth like:
int sum = 0;
for (int i = 0; i < strlen(str); i++) sum += (str[i] - '0');
Serial.println (sum);
Not tested, but it should give you the sum of all bytes in the string.
1. You have this string of characters: Hello World consisting of 11 characters (including the space between Hello and World).
2. If you replace every character of the string of Step-1 by its corresponding ASCII Code (Fig-1), you will come up like this:48 65 6C 6C 6F 20 57 6F 72 6C 64; where, 48 is in hex form for the ASCII Codes (01001000) of H; 65 is the hex form for the ASCII Code of o, and so on. There are 11 bytes of hex formatted data for the 11 charcaters of the 'Hello World' string
Figure-1: ASCII Code Chart for the characters of English Language Alphabet and Control Characters
3. When you declare this array: char msgArray[] = "Hello World";, the array locations are automatically filled up by the ASCII Codes of the characters of the 'Hello World' string. This is to say that location msgArray[0] is filled up by the ASCII Code (01001000 = 0x48) of H, location msgArray[1] is filled up by the ASCII Code (01100101 = 0x65) of e, and so on. At the end, one more location (location msgArray[11]) will be filled up by the ASCII Code (00000000 = 0x00) of the so called NULL Byte.
4. If you want to see the print out of the ASCII Codes of the 'Hello World' string in hex format in the Serial Monitor, you just execute the following sketch. The sketch also prints out the length of the string (11).
1. You have this string of characters: Hello World consisting of 11 characters (including the space between Hello and World).
2. If you replace every character of the string of Step-1 by its corresponding ASCII Code (Fig-1), you will come up like this:48 65 6C 6C 6F 20 57 6F 72 6C 64; where, 48 is in hex form for the ASCII Codes (01001000) of H; 65 is the hex form for the ASCII Code of o, and so on. There are 11 bytes of hex formatted data for the 11 charcaters of the 'Hello World' string
Figure-1: ASCII Code Chart for the characters of English Language Alphabet and Control Characters
3. When you declare this array: char msgArray[] = "Hello World";, the array locations are automatically filled up by the ASCII Codes of the characters of the 'Hello World' string. This is to say that location msgArray[0] is filled up by the ASCII Code (01001000 = 0x48) of H, location msgArray[1] is filled up by the ASCII Code (01100101 = 0x65) of e, and so on. At the end, one more location (location msgArray[11]) will be filled up by the ASCII Code (00000000 = 0x00) of the so called NULL Byte.
4. If you want to see the print out of the ASCII Codes of the 'Hello World' string in hex format in the Serial Monitor, you just execute the following sketch. The sketch also prints out the length of the string (11).
char msgArray[] = "Hello World";
void setup()
{
Serial.begin(9600);
Serial.println(msgArray);
for (int i = 0; i<sizeof(msgArray)-1; i++)
{
Serial.print(msgArray[i], HEX);//excludes NULL byte
}
Serial.println();
Serial.println(sizeof(msgArray)-1, DEC);//excluding NULL byte
}
void loop()
{
}
Thanks, this seems to work but how do I make the HEX and Byte parts into variables I can use? That is the main part im stuck on at this moment
hoekynl:
Thanks, this seems to work but how do I make the HEX and Byte parts into variables I can use? That is the main part im stuck on at this moment
That means that you want pack/insert the parameters (11 and 48656C6C6F20576F726C64) into the indicated/relevant places of the following structure:
GolamMostafa:
That means that you want pack/insert the parameters (11 and 48656C6C6F20576F726C64) into the indicated/relevant places of the following structure:
Parameter
Socket number returned by AT+NSOCR (always 0)
<remote_addr> IPv4 A dot notation IP address (always 172.27.131.100)
<remote_port> A number in the range 0-65535. This is the remote port on which messages will be received (always 15683)
Decimal length of data to be sent
Data received in hex string format
Eventually my arduino (seeeduino lotus) would get temperature, humidity, db levels and weight from a beehive and transfer it via nb-iot so I can monitor it remotely
So what I want to do is insert my information into the function which converts it to HEX and the amount of bytes and then pass it to the command to be sent
hoekynl:
This would be a nice way to do this but myVar.data2Array is not in HEX so it would not work
If the struct, and the items in the struct, are useful, then it is trivial to add a method to the struct to convert the string to the representation that you want.
hoekynl:
This would be a nice way to do this but myVar.data2Array is not in HEX so it would not work
PaulS:
If the struct, and the items in the struct, are useful, then it is trivial to add a method to the struct to convert the string to the representation that you want.
The variable myVar.data2Array (an array) contains the information 'Hello World' (in the high level perception). In the actual low level, the array (data2Array) contains in its every location the 8-bit ASCII Codes (01001000 = 0x48 for H and so on.) for the characters of the message (Hello World). This can be easily (in mathematical language as @PaulS has mentioned -- it is trivial) verified by executing the following code: