cancel
Showing results for 
Search instead for 
Did you mean: 

Checksum code

Former Member
0 Kudos

Hi all,

i need to calculate a checksum of a file in Java..

The old program was written in Delphi and has following code:

unit lib_CRC;

interface

uses
  SysUtils;

function CalculateCRC(Text: String): String;

implementation

{------------------------------------------------------------------------------}

.........

Now my problem is to have the same logic of this code, but written in java... I already tried 3 different programs, but they generate another checksum of the program written in Delphi.

Should I use the same array for the CRC table?

Anyone have experience with this matter?

Thx in advance!

Greets,

Wouter.

Message was edited by: Wouter Dhaenens

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

now I have the error "<i>Type mismatch: cannot convert from byte[] to byte</i>...

This is the line code with error:

byte b = text;

Thx for your help!

Message was edited by: Wouter Dhaenens

former_member182372
Active Contributor
0 Kudos

Hm, SDN forum formatting recognize [ i ] as italic tag. So, it should be byte b = text[ i ] ;

Former Member
0 Kudos

Hi,

thx for your code!

But I still have 1 little problem... there is one error:

Error: The operator ^is undefined for the argument type(s) int, byte[]

Anyone can tell me how to solve this error?

Thanks in advance!

Wouter.

Message was edited by: Wouter Dhaenens

former_member182372
Active Contributor
0 Kudos

Oops,


byte[] text = initialText.getBytes();
for (int i = 0; i < text.length; i++) {
	byte b = text<i>;
	CRCVal = CRCTable [ Math.abs( (byte)( CRCVal ^ b )) ] ^ ((CRCVal>>8) & 0x00FFFFFF);
		}

former_member182372
Active Contributor
0 Kudos

Try this one:


	private static int[] CRCTable =  
		{{------------------------------------------------------------------------------}};
		 
	public static String crc(String initialText) {
		int CRCVal = 0;
		String result;
		byte[] text = initialText.getBytes();
		for (int i = 0; i < text.length; i++) {
			byte b = text<i>;
			CRCVal = CRCTable [ Math.abs( (byte)( CRCVal ^ text<i> )) ] ^ ((CRCVal>>8) & 0x00FFFFFF);
		}

		return Long.toHexString(CRCVal).toUpperCase();
	}

Message was edited by: Maksim Rashchynski