Solved

C# Funtion Calculating UTF-8 BCC

Posted on 2010-08-26
3
1,180 Views
Last Modified: 2013-12-17
Hello guys,
I have a string and convert it into UTF-8 byte array, so this array needs to be an parameter of a function, that will return the BCC. But I need the function.
0
Comment
Question by:IncognitoMan
  • 3
3 Comments
 
LVL 4

Accepted Solution

by:
ricovox earned 500 total points
ID: 33535695
Hello,

I believe by BCC you mean byte,block, and checksum, right?
Can you tell me a little more about the way your data is formatted?

What I mean is that usually you send the data in a transmission that includes additional information like this:
<data...><additional bytes><checksum>

Where "additional bytes" are 1 or 2 additional random bytes that you add to the transmission to ensure better verification accuracy. These would be optional, and maybe you have them and maybe you don't. It depends on your application.

Usually you want to compare the calculated checksum of the data plus the additional bytes AFTER transmission with the checksum calculated BEFORE transmission. Does this sound about right? Do you use the additional bytes or not?

I don't know if the definition for BCC is standardized, but I saw an 8-bit BCC calculated by XORing all the bytes in the data:

private byte CalcBCC(byte[] buffer)
{
    byte calc = 0x00;
   
    //XOR with each byte in the buffer
    for (int i = 1; i < buffer.Length - 1; i++)
    {
        calc ^= buffer[i];
    }
    return calc;
}

You would just have to be consistent in using the same function both before and after transmission to obtain the same checksum. Also make sure to be consistent with what part of the datastream you calculate. I.e. if there are "additional bytes" then make sure to include them in both calculations for consistency.

Hope that helps.
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33535827
After more searching I found another reference about BCC.
This is a PDF that is probably for a specific RS232 instrument.
http://www.bttautomatyka.com.pl/pdf/HA466357.pdf

In it, they have data formatted like this:
<STX><DATA><ETX><CHECKSUM>
Where STX and ETX are each single bytes representing the start and end of the data, respectively.
The BCC is calculated exactly as I specified in the code in my previous post, but it INCLUDES both the STX and ETX. So you have to make sure that these are included in the byte array you pass in to the function. Then you have to remember that the data does NOT include these two characters. The data also does not include the checksum at the end, which is another single byte.

I can send you some more code if you need additional help.
0
 
LVL 4

Expert Comment

by:ricovox
ID: 33535834
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now