parseInt - help!
Istha Powron
Posts: 74
Good morning from Down Under.
I am sending myself completely insane over something that should probably be quite easy. I am simply trying to validate an NMEA stream by calculating a checksum and then comparing it to what the compass sends out. The problem I have is that I don't know how to convert the two byte hex code to an integer. i.e. here is my sentence;
$C264.4P-0.8R0.9T25.2*08
I have calculated the checksum using;
comp_chsum = (byte)InBuffer.charAt(comp_start+1);
for (m = comp_start + 2; m < comp_end; m++)
{
comp_chsum = comp_chsum ^ (byte)InBuffer.charAt(m);
}
where;
comp_start is the position of the '$" at the start of the sentence
comp_end is the position of the '*' before the two digit checksum
However I can't convert the hex *08 to an integer.
I have tried;
CompChk.append(InBuffer.charAt(comp_end+1));
CompChk.append(InBuffer.charAt(comp_end+2));
comp_in_chsum = Integer.parseInt(CompChk.toString(), 16);
Even though the Javelin manual suggests that you can use an "optional radiix" for the parseInt, the compiler doesn't like it. I get the message;
Error:No match was found for method "parseInt(java.lang.String,int)"
This would suggest that you can't have an optional Radiix.
Any ideas on how to convert the 08 to an integer???
Regards,
Istha
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The robot is going to lose. Not by much. But when the final score is tallied, flesh and blood will beat the damn monster.
Adam Smith
I am sending myself completely insane over something that should probably be quite easy. I am simply trying to validate an NMEA stream by calculating a checksum and then comparing it to what the compass sends out. The problem I have is that I don't know how to convert the two byte hex code to an integer. i.e. here is my sentence;
$C264.4P-0.8R0.9T25.2*08
I have calculated the checksum using;
comp_chsum = (byte)InBuffer.charAt(comp_start+1);
for (m = comp_start + 2; m < comp_end; m++)
{
comp_chsum = comp_chsum ^ (byte)InBuffer.charAt(m);
}
where;
comp_start is the position of the '$" at the start of the sentence
comp_end is the position of the '*' before the two digit checksum
However I can't convert the hex *08 to an integer.
I have tried;
CompChk.append(InBuffer.charAt(comp_end+1));
CompChk.append(InBuffer.charAt(comp_end+2));
comp_in_chsum = Integer.parseInt(CompChk.toString(), 16);
Even though the Javelin manual suggests that you can use an "optional radiix" for the parseInt, the compiler doesn't like it. I get the message;
Error:No match was found for method "parseInt(java.lang.String,int)"
This would suggest that you can't have an optional Radiix.
Any ideas on how to convert the 08 to an integer???
Regards,
Istha
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The robot is going to lose. Not by much. But when the final score is tallied, flesh and blood will beat the damn monster.
Adam Smith
Comments
int low = InBuffer.charAt(comp_end+2) - '0';
if (high > 9) high -= 7;
if (low > 9) low -=7;
int checksum = ((high << 4) & 0xF0) | (low & 0x0F);
regards peter
I wouldn't have worked that out in ten years.
Peter, you're a legend.
Thank's,
I
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The robot is going to lose. Not by much. But when the final score is tallied, flesh and blood will beat the damn monster.
Adam Smith