Jump to content

2s complement of a string


crelf

Recommended Posts

So I need to suffix a string with a CRC, and the manual that I'm working with is very sparse on details:

Checksum must be formed and added to each command. The checksum is the two’s complement. The checksum itself is in BYTE format and thus it may rotate over the max value upon calculation.

Yep, that's really about all I know :)

So, for the following ASCII string example:

PLPARM0001:05:0004=00000032

the manual tells me that the 2s complement is 0x2E.

So I know how to do a 2s complement of numbers, and I can think of several ways to do it to a string (or, at least, representations of that string), but does anyone out there know the "right" way to do this before I try to brute force it?

Link to comment

How about this?

post-107-0-98195900-1331745133.png

Nice work todd - that's exactly what I needed! Nothin' like LAVA to feel humbled :)

OT: Does the Code Capture Tool let you grab control/indicators from the front panel too, or did you edit that in?

It lets you grab FP nodes too, but with some property exceptions.

Link to comment

As an aside.......

2's compliment as a CRC really sucks. For example, just try entering the string


PLPARM01:0500000000:0400=32

[/CODE]

Well, one could argue that any CRC sucks, as there aren't completely mutually-exclusive signatures for all possible permutations of a string input. I'm not saying it's the best CRC, but it's better than nothing in most practical cases, and it's what the COTS hardware manufacturer chose, so I'm stuck with it.

Link to comment

Well, one could argue that any CRC sucks, as there aren't completely mutually-exclusive signatures for all possible permutations of a string input.

This is an important thing to remember. Any CRC/checksum method will have multiple messages generate the same result. There are an infinite number of strings that will return the same MD5Sum for example. The likelihood of having two random strings generate the same MD5Sum is 1 in 340,000,000,000,000,000,000,000,000,000,000,000,000 (rounded down). Similar things can be said for GUIDs.

There have been several attempts to break MD5 in a systematic way and I believe someone was able to but I can't seem to find the example at the moment. The example was a EXE that when ran would say "Hello Wold". Then there was a second EXE with the same MD5 and when it was ran it would say something else.

Link to comment

Well, one could argue that any CRC sucks, as there aren't completely mutually-exclusive signatures for all possible permutations of a string input. I'm not saying it's the best CRC, but it's better than nothing in most practical cases, and it's what the COTS hardware manufacturer chose, so I'm stuck with it.

Well. Some are better than others. But a single byte (max 256 permutations) coupled with 2's compliment yields many more collisions than is useful.

Link to comment
Some are better than others.

Agreed.

But a single byte (max 256 permutations) coupled with 2's compliment yields many more collisions than is useful.

Not agreed - it's certainly very weak, but that doesn't mean that it isn't useful as a really gross check. The data that I'm sending/receiving has type checks on either side, so coupled with that, this CRC method is okay as a gross indicator step of a multi-step check. I certainly wouldn't suggest it for important or deterministic streams, especially if it's the only check used.

Anyway, if I'd designed the system I wouldn't have chosen it, but I'm not going to call up the hardware vendor and tell them I refuse to use their hardware just because their CRC isn't up to snuff. :)

Link to comment
  • 2 weeks later...

A weak CRC is better than NO CRC over an unreliable transport.

Remember that for many years (decades) most devices RECEIVING the message were classically very resource or processor limited and simple bitwise checks on the receiver are low overhead.

If the device in question is using anything other than RS232/485 then I'm guessing somebody wrapped a dinosaur in a box, hid a "XYZ to RS232" converter in it and called the product "New and improved" with a price tag disproportionate to the cost.

I can make such a statement only because I've been an unwilling contributor to such a thing. :P

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.