Bonjour,
Depuis ce matin j’essaie de convertir un code qui calcul un CRC16B pour faire du NFC.

J'ai trouvé un bout de code C# qui fonctionne mais a cause des unsigned etc, je n'arrive pas à obtenir le même résultat en JAVA

Voilà le bout de code C# :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace CRC_B
{
    class Program
    {
        static char __crcBDefault = (char)0xffff;
        private static char UpdateCrc(byte b, char crc)
        {
            byte ch = (byte)(b ^ (byte)(crc & 0x00ff));
            ch = (byte)(ch ^ (ch << 4));
            return (char)((crc >> 8) ^ (ch << 8) ^ (ch << 3) ^ (ch >> 4));
        }
 
        public static char ComputeCrc(byte[] bytes)
        {
            char res = __crcBDefault;
            for (int i = 0; i < bytes.Length; i++)
            {
                res = UpdateCrc(bytes[i], res);
            }
 
 
            return (char)~res;
        }
 
        static void Main(string[] args)
        {
            // test case 1 0xFC, 0xD1 
            var bytes = new byte[] { 0x0F, 0xAA, 0xFF };
            char crc = ComputeCrc(bytes);
            byte[] cbytes = BitConverter.GetBytes(crc);
            Console.WriteLine("First (0xFC): {0:X}\tSecond (0xD1): {1:X}", cbytes[0], cbytes[1]);
 
            // test case 2 0xCC, 0xC6 
            bytes = new byte[] { 0x00, 0x00, 0x00 };
            crc = ComputeCrc(bytes);
            crcint = crc;
            cbytes = BitConverter.GetBytes(crc);
            Console.WriteLine("First (0xCC): {0:X}\tSecond (0xC6): {1:X}", cbytes[0], cbytes[1]);
 
            Console.ReadLine();
        }
    }
}
Et le code que j'ai converti en JAVA mais qui ne me donne pas de résultat satisfaisant
J'ai un soucis avec les unsigned, par exemple la ligne "(byte)(crc & 0x00ff)" me retourne -16 en Java et 255 en C#...
Si j'utilise un char à la place du byte ça fonctionne mais le calcul d'après devient incorrect.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
package mypackage;
 
public class NFCUtils 
{
 
	static final char __crcBDefault = 0xffff; 
 
	private static char UpdateCrc(byte b, char crc) 
	{ 
		char ch = (char)(b ^ (byte)(crc & 0x00ff));
		char ret =  (char)((crc >> 8)^(ch << 8)^(ch << 3)^(ch >> 4));
		return ret;
	} 
 
	public static char ComputeCrc(byte[] bytes) 
	{ 
		char res = __crcBDefault; 
		for (int i = 0; i < bytes.length; i++) 
		{
			res = UpdateCrc(bytes[i], res); 
		}
 
		return (char) ~res; 
	} 
 
}
Merci d'avance pour votre aide je ne sais plus trop comment faire