-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBIN2C.PAS
58 lines (54 loc) · 1.42 KB
/
BIN2C.PAS
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
50
51
52
53
54
55
56
57
58
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2021
@website(https://www.gladir.com/dev-cools)
@abstract(Target: Turbo Pascal, Free Pascal)
}
Program BIN2C;
Var
Source:File{$IFDEF FPC} of Byte{$ENDIF};
Target:Text;
Tampon:Array[0..0]of Byte;
ByteReaded:Integer;
Position:Byte;
Function ByteHex2Str(value:Byte):String;
Const
matrix:Array[0..15]of Char = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
Begin
ByteHex2Str:=matrix[(value shr 4) and $0F]+matrix[value and $F];
End;
BEGIN
If(ParamStr(1)='/?')or(ParamStr(1)='--help')or(ParamStr(1)='-h')Then Begin
WriteLn('BIN2C : Cette commande permet de convertir un fichier binaire en tableau de C.');
WriteLn;
WriteLn('Syntaxe : BIN2C fichier fichier.c');
End
Else
If ParamCount=2Then Begin
Assign(Source,ParamStr(1));
Reset(Source);
Assign(Target,ParamStr(2));
Rewrite(Target);
Position:=0;
WriteLn(Target,'const unsigned char samples[',FileSize(Source),'] = {');
Write(Target,' ');
While Not EOF(Source)do Begin
BlockRead(Source,Tampon,1,ByteReaded);
Write(Target,'0x',ByteHex2Str(Tampon[0]));
If Not EOF(Source)Then Begin
Write(Target,', ');
Inc(Position);
If Position=8Then BEgin
WriteLn(Target);
Write(Target,' ');
End;
Position:=Position and 7;
End;
End;
WriteLn(Target);
WriteLn(Target,'};');
Close(Target);
Close(Source);
End
Else
WriteLn('Parametre invalide !');
END.