snippets:calcul_du_checksum_des_trames_de_teleinfo
Différences
Ci-dessous, les différences entre deux révisions de la page.
snippets:calcul_du_checksum_des_trames_de_teleinfo [28/04/2024 08:33] – créée antoineve | snippets:calcul_du_checksum_des_trames_de_teleinfo [28/04/2024 12:16] (Version actuelle) – antoineve | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
<code python> | <code python> | ||
def checksum(frame): | def checksum(frame): | ||
- | somme = 0 | + | |
- | for c in frame[: | + | for c in frame[: |
- | somme += ord(c) | + | somme += ord(c) |
- | | + | somme_trunc = bin(somme)[-6: |
- | | + | calc_check = chr(32 + int(somme_trunc, |
- | if calc_check == frame[-1: | + | if calc_check == frame[-1: |
- | return True | + | return True |
- | | + | else: |
- | return False | + | return False |
+ | </ | ||
+ | ===== JavaScript ===== | ||
+ | <code javascript> | ||
+ | function checksum(frame) { | ||
+ | let somme = 0; | ||
+ | for (let i = 0; i < frame.length - 1; i++) { | ||
+ | somme += frame.charCodeAt(i); | ||
+ | } | ||
+ | const sommeTrunc = (somme & 0b111111).toString(2); | ||
+ | const calcCheck = String.fromCharCode(32 + parseInt(sommeTrunc, | ||
+ | if (calcCheck === frame.slice(-1)) { | ||
+ | return true; | ||
+ | } else { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | |||
snippets/calcul_du_checksum_des_trames_de_teleinfo.1714293194.txt · Dernière modification : 28/04/2024 08:33 de antoineve