Softwareentwicklung
›
Thread
Mehre ints speichern in char array und lesen
HH
Halgn H.
Gast
10.01.2012 09:55
#2494094
Hallo,
Möchte Zwei einstelige int und zwei dreistelige in char sepichern so in
etwa: 1 char Entity [] = { '1' , '1' , '0' , '0' , '1' , '0' , '0' , '1' }
ich brauch ein Weg wie ich es speicher -> Entity_SetData()
und ein weg zum lesen Entity_GetData()
ich weiß aber nicht wie ich das mit dem 001 bis 999 hinkriege ;C
8 byte groß
MfG TheNightAlex
B
Backupper
Gast
10.01.2012 10:04
#2494100
Wenn ich das richtig sehe willst du die Zahlen als ASCII String
speichern, nicht binär.
Dann musst du dir Gedanken machen wie du erkennst wo eine Zahl aufhört
und die nächste anfängt.
Das übliche: Schildere dein Problem nicht deine vermeintliche Lösung.
Siehe Netiquette
HH
Halgn H.
Gast
10.01.2012 10:49
#2494184
Ok hab nun es Fast geschaft das problem ist jetzt wenn ich in main 1 Entity_SetData ( 0 , 2 , 10 );
2
3 Entity_SetData ( 0 , 0 , 1 );
4
5 printf ( "Entity: \n %s \n %d \n " , Entity , Entity_GetData ( 0 , 2 ));
schreibe bekomme ich in der console
1 0 1 9
2 Entity:
3 10019000000000000000000000000000000000000000000000000000000000000000000000000000
4 00000000000000000000000000000000000000000000000
5 19
er macht aus der 10-> 19!!!
Sourcecode 1 /** Entry Steuerung */
2 #define Max_Entity 128
3 char Entity [ 8 * Max_Entity ];
4
5 void Entity_Init ()
6 {
7 int n ;
8
9 /* Entity füllen mit Nullen */
10 for ( n = 0 ; n < Max_Entity - 1 ; n ++ )
11 Entity [ n ] = '0' ;
12
13 /* Ende setzen SONST fehler! */
14 Entity [ Max_Entity - 1 ] = '\0' ;
15 }
16
17 void Entity_Process ()
18 {
19 /* ☻ */
20
21
22 }
23
24 int Entity_GetData ( int ID , int Index )
25 {
26 int a , b , c ;
27
28 /* Falls es eine 3 stellige zahl ist */
29 if ( Index == 2 )
30 {
31 a = Entity [ ID * 8 + Index ] - 48 ;
32 b = Entity [ ID * 8 + Index + 1 ] - 48 ;
33 c = Entity [ ID * 8 + Index + 2 ] - 48 ;
34
35 return ( a * 100 ) + ( b * 10 ) + c ;
36 }
37 else if ( Index == 3 )
38 {
39 a = Entity [ ID * 8 + Index + 3 ];
40 b = Entity [ ID * 8 + Index + 3 + 1 ];
41 c = Entity [ ID * 8 + Index + 3 + 2 ];
42
43 return ( a * 100 ) + ( b * 10 ) + c - 48 ;
44 }
45 /* Rückgabe einer Zahl */
46 return Entity [ ID * 8 + Index ] - 48 ;
47 }
48
49 void Entity_SetData ( int ID , int Index , int Data )
50 {
51 int a , b , c ;
52
53 if ( Index == 2 )
54 {
55 a = Data / 100 ;
56 b = ( Data - a ) / 10 ;
57 c = Data - a - b ;
58 Entity [ ID * 8 + Index ] = a + 48 ;
59 Entity [ ID * 8 + Index + 1 ] = b + 48 ;
60 Entity [ ID * 8 + Index + 2 ] = c + 48 ;
61 printf ( "%d %d %d \n " , a , b , c );
62 }
63 else if ( Index == 3 )
64 {
65 a = Data / 100 ;
66 b = ( Data - a ) / 10 ;
67 c = Data - a - b ;
68 Entity [ ID * 8 + Index + 3 ] = a + 48 ;
69 Entity [ ID * 8 + Index + 3 + 1 ] = b + 48 ;
70 Entity [ ID * 8 + Index + 3 + 2 ] = c + 48 ;
71 }
72 else Entity [ ID * 8 + Index ] = Data + 48 ;
73 }
HH
Halgn H.
Gast
10.01.2012 12:02
#2494307
Hab jetzt endlich gebugfixed...
wenn jemand dern Sourcecode will... hier
Main: 1 /* Entity erstellen */
2 Entity_Init ();
3
4 Entity_SetData ( 0 , 0 , 1 );
5 Entity_SetData ( 0 , 1 , 0 );
6 Entity_SetData ( 0 , 2 , 256 );
7 Entity_SetData ( 0 , 3 , 256 );
8
9 Entity_SetData ( 1 , 0 , 2 );
10 Entity_SetData ( 1 , 1 , 0 );
11 Entity_SetData ( 1 , 2 , 512 );
12 Entity_SetData ( 1 , 3 , 256 );
13
14 printf ( "Entity: \n %s \n %d \n " , Entity , Entity_GetData ( 1 , 2 ));
Entity.c: 1 /** Entry Steuerung */
2 #define Max_Entity 128
3 char Entity [ 8 * Max_Entity ];
4
5 void Entity_Init ()
6 {
7 int n ;
8
9 /* Entity füllen mit Nullen */
10 for ( n = 0 ; n < Max_Entity - 1 ; n ++ )
11 Entity [ n ] = '0' ;
12
13 /* Ende setzen SONST fehler! */
14 //Entity[Max_Entity-1] = '\0';
15 }
16
17 void Entity_Process ()
18 {
19 /* ☻ */
20
21
22 }
23
24 int Entity_GetData ( int ID , int Index )
25 {
26 int a , b , c ;
27
28 /* Falls es eine 3 stellige zahl ist */
29 if ( Index == 2 )
30 {
31 /* Einzelne char Zeichen auslesen und wieder zu int machen */
32 /* 48 = '0' */
33 a = Entity [ ID * 8 + Index ] - 48 ;
34 b = Entity [ ID * 8 + Index + 1 ] - 48 ;
35 c = Entity [ ID * 8 + Index + 2 ] - 48 ;
36
37 /* Die Zahl wieder zusamensetzen */
38 return ( a * 100 ) + ( b * 10 ) + c ;
39 }
40 else if ( Index == 3 )
41 {
42 /* Einzelne char Zeichen auslesen und wieder zu int machen */
43 /* 48 = '0' */
44 a = Entity [ ID * 8 + Index + 2 ] - 48 ;
45 b = Entity [ ID * 8 + Index + 2 + 1 ] - 48 ;
46 c = Entity [ ID * 8 + Index + 2 + 2 ] - 48 ;
47
48 /* Die Zahl wieder zusamensetzen */
49 return ( a * 100 ) + ( b * 10 ) + c ;
50 }
51 /* Rückgabe einer Zahl */
52 return Entity [ ID * 8 + Index ] - 48 ;
53 }
54
55 void Entity_SetData ( int ID , int Index , int Data )
56 {
57 int a , b , c ;
58
59 /* X Achse? */
60 if ( Index == 2 )
61 {
62 /* Die X und Y achse in dreistelige char ausernander nehmen*/
63 a = Data / 100 ;
64 b = ( Data - ( a * 100 ) ) / 10 ;
65 c = Data - a * 100 - b * 10 ;
66
67 /* Immer nocht int -> ab zu char und speichern */
68 /* 48 ist '0' */
69 Entity [ ID * 8 + Index ] = a + 48 ;
70 Entity [ ID * 8 + Index + 1 ] = b + 48 ;
71 Entity [ ID * 8 + Index + 2 ] = c + 48 ;
72 }
73 /* Y Achse? */
74 else if ( Index == 3 )
75 {
76 /* Die X und Y achse in dreistelige char ausernander nehmen*/
77 a = Data / 100 ;
78 b = ( Data - ( a * 100 ) ) / 10 ;
79 c = Data - a * 100 - b * 10 ;
80
81 /* Immer nocht int -> ab zu char und speichern */
82 /* 48 ist '0' */
83 Entity [ ID * 8 + Index + 2 ] = a + 48 ;
84 Entity [ ID * 8 + Index + 2 + 1 ] = b + 48 ;
85 Entity [ ID * 8 + Index + 2 + 2 ] = c + 48 ;
86 }
87 /* Einzelne Zahl einfach so Speichern */
88 else Entity [ ID * 8 + Index ] = Data + 48 ;
89 }
D
deutschleerer
Gast
10.01.2012 12:07
#2494321
Alexander Ljubizki schrieb:
> gebugfixed
>
... man ist das eine bekloppte Wortkreation, sehr schön ist auch die
Wahl der Zeitform.
Mahlzeit (und Entschuldigung für etwas OT)
D
DirkB
Gast
10.01.2012 12:10
#2494325
Schreib statt 48 doch was du wirklich meinst: '0'
Antwort schreiben
Bitte melde dich an, um einen Beitrag zu schreiben.
Noch kein Account?
Die Registrierung ist kostenlos und dauert nur eine Minute.
Jetzt registrieren