Calcul du CRC 16
Résolu
cocodu67...
Messages postés
3178
Date d'inscription
Statut
Membre
Dernière intervention
-
cocodu67... Messages postés 3178 Date d'inscription Statut Membre Dernière intervention -
cocodu67... Messages postés 3178 Date d'inscription Statut Membre Dernière intervention -
A voir également:
- Excel crc16
- Iphone 16 - Accueil - Téléphones
- Calcul moyenne excel - Guide
- Calcul km marche à pied gratuit - Télécharger - Sport
- Calcul charpente bois gratuit - Télécharger - Architecture & Déco
- Logiciel gratuit calcul valeur nutritionnelle - Télécharger - Santé & Bien-être
2 réponses
Salut, le hic dès le début c'est qu'il existe plusieurs CRC-16... Après vérif ici c'est du CRC-16 Modbus que tu parles.
Deuxièmement,
Après une petite recherche Google je tombe sur ça, et un tout petit algo qui fait ce qu'on veut, mais il est en C.
Vite fait traduit en C#:
from human import idiocy
del idiocy
Deuxièmement,
CRC16(0x16) = 0x533F, c'est pas ce que tu cherches: c'est
3Aen hexa certes, mais tu parles d'une chaîne de caractère, donc les données en hexa sont
0x33 0x41, et là
CRC16("3A") = 0x70D5.
Après une petite recherche Google je tombe sur ça, et un tout petit algo qui fait ce qu'on veut, mais il est en C.
Vite fait traduit en C#:
static class CRC { static uint CRC16(byte[] buf) { uint crc = 0xFFFF; for (int pos = 0; pos < buf.Length; pos++) { crc ^= (uint)buf[pos]; // XOR byte into least sig. byte of crc for (int i = 8; i != 0; i--) { // Loop over each bit if ((crc & 0x0001) != 0) { // If the LSB is set crc >>= 1; // Shift right and XOR 0xA001 crc ^= 0xA001; } else // Else LSB is not set crc >>= 1; // Just shift right } } return crc; } static void Main(string[] args) { byte[] data = System.Text.Encoding.ASCII.GetBytes("3A"); System.Console.WriteLine(CRC16(data).ToString("X")); } }
from human import idiocy
del idiocy
C'est parfait, ça fonctionne.
Pour ceux que ça intéresse, ma fenêtre est composée d'une textBoxSaisie, d'un labelAffichage et d'un buttonCalcul
Voici le code entier :
Merci à toi gravgun, j'ai enfin compris comment ça fonctionne pour calculer le CRC 16.
Bonne journée
Pour ceux que ça intéresse, ma fenêtre est composée d'une textBoxSaisie, d'un labelAffichage et d'un buttonCalcul
Voici le code entier :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplicationCRC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalcul_Click(object sender, EventArgs e)
{
byte[] data = System.Text.Encoding.ASCII.GetBytes(textBoxSaisie.Text);
labelAffichage.Text = (CRC16(data).ToString("X"));
}
static uint CRC16(byte[] buf)
{
uint crc = 0xFFFF;
for (int pos = 0; pos < buf.Length; pos++)
{
crc ^= (uint)buf[pos];
for (int i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
return crc;
}
}
}
Merci à toi gravgun, j'ai enfin compris comment ça fonctionne pour calculer le CRC 16.
Bonne journée
Cependant, je ne comprend pas à quoi sert le .ToString("X")
Cela sert simplement à dire que c'est une valeur hexadécimale ?
Bonne soirée :)
(Et pourquoi j'ai mis moi? Je voulais dire )