Link to home
Start Free TrialLog in
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia

asked on

Help required to convert a C++ Code to Ms Access VBA

Is it possible to get assistance on converting the CRC  Code below which was done in C++ to MS Access VBA


unsigned short int cal_crc(unsigned char *ptr, unsigned int len)
{
unsigned char i;
unsigned int crc=0;
while(len--!=0)
{
for(i=0x80; i!=0; i/=2)
{
if((crc&0x8000)!=0)
{
crc*=2;
crc^=0x18005;
}
else
{
crc*=2;
}
if((*ptr&i)!=0)
crc^=0x18005;
}
ptr++;
}
return(crc);

Open in new window

Avatar of Bill Prew
Bill Prew

Can you provide some test data and the desired CRC results to be calculated from that input?


»bp

If your goal is to calculate the CRC for a given string or file, perhaps this would work:


http://www.freevbcode.com/ShowCode.asp?ID=655


Also, FMS Total Visual Sourcebook has functions that do this. TVS is a purchased product, but it has a LOT of code and can save you tons of time:


https://www.fmsinc.com/MicrosoftAccess/modules/index.asp

Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

ASKER

Dear Bill Prew;

Kindly see below the information provided from the INSPUR ESD Device below:

The contents of the interface protocol include three commands (request/response), such as Get Status, Invoice signing and error message. All the data will be organized in JSON format starting with package header and ending with checksum. It consists of Header, Command ID, Length of data, Content and Verification Code (CRC):

Detailed description of the device:

Field
Header1    = 1 Length Byte (The first byte of package header 0x1A
Header2    = 1 Length Byte (The first byte of package header 0x5D
CmdID       = 1 Command IDs : (0x01 acquire the status of the device, 0x02 invoice signing, 0x03 Error code)
Length       = 4  The length of the content, big-endian
Content  = ? Data to be sent
CRC = 2 (Two-Byte verification (CRC), it will be generated by bytes start from Header to Content)

Regards
Kindly see below the information provided from the INSPUR ESD Device below:

That didn't answer my question, I am looking for some test data to know if the converted VBA routine is working properly, that is what I wanted you to provide.

For example "0123456789" seems  to generate a crc value of 64788, is that correct, and what you seek?


»bp
Yes Bill Prew that is very correct I have asked the provider of the gadget.

Regards

Chris
Dear Bill Prew;

Kindly note that the CRC will be changing according to the information sent as per given parameters below:


 Field

Header1    = 1 Length Byte (The first byte of package header 0x1A
Header2    = 1 Length Byte (The first byte of package header 0x5D
CmdID       = 1 Command IDs : (0x01 acquire the status of the device, 0x02 invoice signing, 0x03 Error code)
Length       = 4  The length of the content, big-endian
Content  = ? Data to be sent
CRC = 2 (Two-Byte verification (CRC), it will be generated by bytes start from Header to Content)


For example the Content  = ? Data to be sent  

May have a length of 864 bytes or more depending to the invoice lines created or even less


Sorry for that.

Regards

Chris
Dear Bill Prew;

Any lucky there!

Regards

Chris
This will take time to work up, there are several critical differences between C++ and VBA that need to be accounted and adjusted for, and it will take time to work out.  A couple that have already come to mind include:

  • VBA does not support unsigned number data types
  • C++ allows overflow of number data types and discards the overflow, VBA does not and throws an error

I'm sure there will be other considerations I am not thinking of right now...


»bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Bill Prew


Thank you so much for a great job well done , this has a big headache for me and now its over.

Regards

Chris