Shop OBEX P1 Docs P2 Docs Learn Events
parseInt - help! — Parallax Forums

parseInt - help!

Istha PowronIstha Powron Posts: 74
edited 2010-01-28 12:09 in General Discussion
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

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2010-01-28 09:18
    int high = InBuffer.charAt(comp_end+1) - '0';
    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
  • Istha PowronIstha Powron Posts: 74
    edited 2010-01-28 12:09
    Nah,

    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
Sign In or Register to comment.