Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:05:43+00:00 2026-05-27T14:05:43+00:00

we are sending some data over a serial line, and i can do pretty

  • 0

we are sending some data over a serial line, and i can do pretty much everything via a bash script (instead of code), except for the crc16 calculation. if i can do it all in scripts versus code, it would make configuration a heckofalot easier (especially while in the field).

i’m alright with commands, but i lose all ability when we get to the tricky stuff.

so my question is, can someone do a rewrite of this CRC16 for me, but within bash?

here is the algorithm grabbed from wikipedia, and it is the one in our code:

uint16_t Encoder::checksum(std::string thestring)
{
    uint8_t d, e, f;
    uint16_t c, r, crccalc;
    c = 0xffff;

    for (unsigned int i = 0; i < thestring.length(); i++)
    {
        d = thestring[i];
        e = c ^ d;
        f = e ^ (e << 4);
        r = (c >> 8) ^ (f << 8) ^ (f << 3) ^ (f >> 4);
        c = r;
    }
    c ^= 0xffff;
    crccalc = c;
    return crccalc;
}

i can easily create an executable out of the C++ code, and just feed it stdin, but i think it would be really neat to be able to have this within the bash.

the other thing i don’t know is how to ensure that my variable sizes are correct. how can i ensure that i am getting a 16 bit integer?

any help would be great. i found a little script online, but i didn’t trust it. thought it would be really cool to have answered here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:05:43+00:00Added an answer on May 27, 2026 at 2:05 pm

    Bash have:

    • xor ($((5^2)) will be 7);
    • left shift ($(3<<2) will be 12);
    • right shift ($(8>>2) will be 2);
    • hexademical numbers support ($((0xFF)) will be 255).

    Nothing comes to mind to convert from 32 (64) to 16 bit integer in pure Bash but you can do it with awk:

    $ echo 65536 | awk '{printf("%hu\n",$1)}'
    0
    

    This should be enough to rewrite algorithm in Bash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sending/receiving data over a serial line in Linux and I would like
I'm sending some data over AJAX to a PHP file. It's about filter options.
Suppose we're sending some data over the network ... it is my understanding that
Im sending over some json data and assigning to a hidden field using: @Html.Hidden(hidden-places,
I'm sending some data to an external URL using Curl. The server sends me
I have written some jQuery ajax code where I am sending a request to
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
I've seen lots of examples of sending serialized data over sockets in Java, but
I'm sending through a socket from java to flex client some data and the
I've been doing some thinking about data redundancy, and just wanted to throw everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.