Skip to content

Instantly share code, notes, and snippets.

@z81
Forked from carbide-public/ test.carbide.md
Last active August 30, 2016 14:39
Show Gist options
  • Save z81/ff0128dbbbcfc6eb078b0b67a31f34f6 to your computer and use it in GitHub Desktop.
Save z81/ff0128dbbbcfc6eb078b0b67a31f34f6 to your computer and use it in GitHub Desktop.
hex
import fs from 'fs';
const calcCrs = row => {
let sum = 0;
console.log(row)
for(let i = 1; i < row.length - 2; i += 2) {
sum += parseInt(row.substr(i, 2), 16);
}
return (0x10000000 - sum).toString(16).substr(-2).toUpperCase();
};
const buildRow = row => {
return `:${row.length.toString(16).toUpperCase()}${row.adress}${row.type}${row.data}${row.crs}`;
}
const parseRow = row => {
let startSymbol = row[0],
length = parseInt(row.substr(1, 2), 16),
data = row.substr(9, length * 2),
crs = row.substr(row.length - 2),
adress = row.substr(3, 4),
type = row.substr(7, 2);
console.log(row, length)
if (startSymbol !== ':') return { error: 'Invalid format #0' };
return {
length,
data,
crs,
adress,
type
};
}
const normalizeRow = row => {
const MIN_LENGTH = 16;
if (row.data.length < MIN_LENGTH * 2 && row.type === '00') {
row.data += 'F'.repeat(MIN_LENGTH * 2 - row.data.length );
row.length = MIN_LENGTH;
row.oldCrs = row.crs;
row.crs = calcCrs(buildRow(row));
}
return row;
};
const data = `:10010000214601360121470136007EFE09D2190140
:100110002146017EB7C20001FF5F16002148011988
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:107F300051F7F601A7BEE89507B600FCFDCF97BE46
:0A0420000000F9CF0895F894FFCF13
:047FF000FF270994CA
:027FFE000467
:0400000300007E007B
:00000001FF`;
const rowsList = data.split("\n");
let res2 = [];
let res = '';
for(let i = 0; i < rowsList.length; i++) {
const row = normalizeRow(parseRow(rowsList[i]));
res += buildRow(row) + '\n';
res2.push(row);
}
res;
res2;
:100010000C946E000C946E000C946E000C946E00A8
:100020000C946E000C946E000C946E000C946E0098
:100030000C946E000C946E000C946E000C946E0088
:100040000C9488000C946E000C946E000C946E005E
:100050000C946E000C946E000C946E000C946E0068
:100060000C946E000C946E00000000080002010069
:100070000003040700000000000000000102040863
:100080001020408001020408102001020408102002
:10009000040404040404040402020202020203032E
:1000A0000303030300000000250028002B000000CC
:1000B0000000240027002A0011241FBECFEFD8E043
:1000C000DEBFCDBF21E0A0E0B1E001C01D92A930AC
:1000D000B207E1F70E9403020C9413020C94000093
:1000E00061E08DE00C94930161E08DE00E94CC0111
:1000F00068EE73E080E090E00E94F50060E08DE043
:100100000E94CC0168EE73E080E090E00C94F50072
:100110001F920F920FB60F9211242F933F938F933C
:100120009F93AF93BF938091010190910201A091A1
:100130000301B09104013091000123E0230F2D371A
:1001400020F40196A11DB11D05C026E8230F0296DB
:10015000A11DB11D20930001809301019093020124
:10016000A0930301B09304018091050190910601D1
:10017000A0910701B09108010196A11DB11D8093C6
:10018000050190930601A0930701B0930801BF9168
:10019000AF919F918F913F912F910F900FBE0F9034
:1001A0001F9018953FB7F894809105019091060132
:1001B000A0910701B091080126B5A89B05C02F3F6B
:1001C00019F00196A11DB11D3FBF6627782F892F19
:1001D0009A2F620F711D811D911D42E0660F771FDE
:1001E000881F991F4A95D1F708958F929F92AF92D9
:1001F000BF92CF92DF92EF92FF926B017C010E943F
:10020000D2004B015C01C114D104E104F104F1F00E
:100210000E9412020E94D200681979098A099B097A
:10022000683E73408105910570F321E0C21AD10840
:10023000E108F10888EE880E83E0981EA11CB11C2D
:10024000C114D104E104F10429F7DDCFFF90EF9050
:10025000DF90CF90BF90AF909F908F90089578944B
:1002600084B5826084BD84B5816084BD85B58260BB
:1002700085BD85B5816085BDEEE6F0E08081816059
:100280008083E1E8F0E01082808182608083808159
:1002900081608083E0E8F0E0808181608083E1EB31
:1002A000F0E0808184608083E0EBF0E08081816019
:1002B0008083EAE7F0E080818460808380818260CF
:1002C00080838081816080838081806880831092B8
:1002D000C1000895833081F028F4813099F0823094
:1002E000A1F008958730A9F08830B9F08430D1F4B6
:1002F000809180008F7D03C0809180008F778093F4
:100300008000089584B58F7702C084B58F7D84BD49
:1003100008958091B0008F7703C08091B0008F7DE9
:100320008093B0000895CF93DF9390E0FC01E458F0
:10033000FF4F2491FC01E057FF4F8491882349F13E
:1003400090E0880F991FFC01E255FF4FA591B491F1
:100350008C559F4FFC01C591D4919FB7611108C086
:10036000F8948C91209582238C93888182230AC0F3
:10037000623051F4F8948C91322F309583238C9312
:100380008881822B888304C0F8948C91822B8C9373
:100390009FBFDF91CF9108950F931F93CF93DF936A
:1003A0001F92CDB7DEB7282F30E0F901E859FF4F93
:1003B0008491F901E458FF4F1491F901E057FF4F80
:1003C00004910023C9F0882321F069830E946A0107
:1003D0006981E02FF0E0EE0FFF1FEC55FF4FA59174
:1003E000B4919FB7F8948C91611103C0109581234B
:1003F00001C0812B8C939FBF0F90DF91CF911F91F4
:100400000F91089508950E942F010E9402020E94F8
:100410007000C0E0D0E00E9474002097E1F30E94D9
:0A0420000000F9CF0895F894FFCF13
:107E0000112484B714BE81FFF0D085E080938100F7
:107E100082E08093C00088E18093C10086E0809377
:107E2000C20080E18093C4008EE0C9D0259A86E02C
:107E300020E33CEF91E0309385002093840096BBD3
:107E4000B09BFECF1D9AA8958150A9F7CC24DD24C4
:107E500088248394B5E0AB2EA1E19A2EF3E0BF2EE7
:107E6000A2D0813461F49FD0082FAFD0023811F036
:107E7000013811F484E001C083E08DD089C08234E0
:107E800011F484E103C0853419F485E0A6D080C0E4
:107E9000853579F488D0E82EFF2485D0082F10E0AE
:107EA000102F00270E291F29000F111F8ED06801E7
:107EB0006FC0863521F484E090D080E0DECF843638
:107EC00009F040C070D06FD0082F6DD080E0C81688
:107ED00080E7D80618F4F601B7BEE895C0E0D1E017
:107EE00062D089930C17E1F7F0E0CF16F0E7DF06D8
:107EF00018F0F601B7BEE89568D007B600FCFDCFD4
:107F0000A601A0E0B1E02C9130E011968C91119780
:107F100090E0982F8827822B932B1296FA010C0160
:107F200087BEE89511244E5F5F4FF1E0A038BF0790
:107F300051F7F601A7BEE89507B600FCFDCF97BE46
:107F4000E89526C08437B1F42ED02DD0F82E2BD052
:107F50003CD0F601EF2C8F010F5F1F4F84911BD097
:107F6000EA94F801C1F70894C11CD11CFA94CF0C13
:107F7000D11C0EC0853739F428D08EE10CD085E9AC
:107F80000AD08FE07ACF813511F488E018D01DD067
:107F900080E101D065CF982F8091C00085FFFCCF94
:107FA0009093C60008958091C00087FFFCCF809118
:107FB000C00084FD01C0A8958091C6000895E0E648
:107FC000F0E098E1908380830895EDDF803219F02E
:107FD00088E0F5DFFFCF84E1DECF1F93182FE3DFCA
:107FE0001150E9F7F2DF1F91089580E0E8DFEE27F6
:047FF000FF270994CA
:027FFE00040479
:0400000300007E007B
:00000001FF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment