Hi,
is there a function to convert a string like "1101000001101101111110111110" (two's complement) to a signed int?
Thanks
Looks more like a signed long to me
Yeah, you're right, but anyhow... is there a simple way to convert?
strtol,() or strtoul,()
looks like stoi() has a radix option (i never knew that. thanks for asking)
I can't see it solves the two's complement thing.
Also I can't get stoi() working. (stoi was not declared)
looks like stoi() is a C++ thing and needs
#include <string>
using namespace std;
This seems not to work. Also I don't think it handles the two's complement.
Slightly bizarre results - "strtol" doesn't seem to work (e.g. "10000000000000000000000000000000" returns 0x7fffffff), but "strtoul" assigned to a signed long does.
Need to investigate (probably done something dumb)...
leclerke:
is there a function to convert a string like "1101000001101101111110111110" (two's complement) to a signed int?
How does one end up with an ASCII string representation of a binary number in the first place? That's a pretty inefficient way of moving data around -- 33 bytes (with null termination) to hold a value that will fit in 4 bytes.
Actually I'm trying to decode AIS Messages...
leclerke:
Actually I'm trying to decode AIS Messages...
Post a clickable link to the reference.
leclerke:
Actually I'm trying to decode AIS Messages...
In my world AIS is Alarm Indication Signal and is all 1s. I suspect you mean something else.
Signed ints ARE in 2s compliment.
leclerke:
AIVDM/AIVDO protocol decoding
OK, so the payload is sent as ASCII characters each representing a 6-bit symbol (i.e. Base 64). That reference shows the mapping. But that doesn’t explain why / how you now have the data represented by an ASCII string of 1s and 0s.
It would be better to simply get the numeric value of each field using the technique described under “AIVDM/AIVDO Payload Armoring” and then use bit manipulation to shift them around into their final format depending on payload type (integer, float, string, etc).
BTW, THIS is a clickable link: AIVDM/AIVDO protocol decoding
This is NOT: AIVDM/AIVDO protocol decoding
I'm decoding the payload char by char to a binary string.
String data = getPayload(s);
//Serial.println(data);
String binary = "";
for (int i = 0; i < data.length() + 1; i++) {
binary += decode(data[i]);
}
String decode(char inByte) {
//Payload Armoring
String dec = "000000";
switch (inByte) {
case '0':
dec = "000000";
break;
case '1':
dec = "000001";
break;
...
}
return dec;
}
leclerke:
I'm decoding the payload char by char to a binary string.
Hence, my question. Why?
I don't know how else to do. And except the negative values for longitude and latitude it works so far.
Like I said in Reply #14, I'd use the provided algorithm to extract each 6-bit "nibble" as a numeric value. Then, the actual integer results can be obtained with bit shifting and bit-wise operations. I saw on the web page that numbers are represented in Big Endian format. Arduino uses Little Endian. You'll have to take care of that. There are also issues of padding required to obtain the 8 bit alignment.