Oct 25, 2016 at 5:11pm Oct 25, 2016 at 5:11pm UTC
Hei together
I want to read a data with binarys in. then i want to print them out in Octets(bytes but here I have to name them octets). Now I startet, and everything went great, but only for the first octet, that's right.
the second Octet is wrong. But I only realized is when I printed the whole data in one string to compare. when i print the whole string, the the octets change! So i think both Problems are part of a bigger problem, I cant localize.
Here my code:
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 59 60 61 62 63
void buildOctets(int n, int m, char * c, char * buffer){
int i = 0;
for (int j= 0;j<n;j++){
for (int k = 0;k<m;k++){
c[k+j*m]=buffer[i];
i++;
}
}
}
void PrintOctet(char * octet){
for (int i = 0; i<8;i++){
printf("%c" ,octet[i]);
}
}
void PrintOctets16(char * c){
for (int i = 0; i<128;i++){
PrintOctet(&c[i]);
printf("\t" );
if (i%2==1){
printf("\n" );
}
}
}
int main() {
ifstream file("/home/florian/workspace/Dec1/Debug/data.dat" , std::ios::binary);
if (!file)
{
cout << "file not open\n" ;
return 1;
}
char *buffer = new char [8*128];
if (!file.read(buffer, 8*128))
{
cout << "file read error\n" ;
return 2;
}
// char * cPointer;
//cPointer = new char[128*8];
char c[128][8];
char * cPointer = &c[0][0];
//füllt buffer in c rein
buildOctets(128,8,cPointer,buffer);
//printf("%s",&cPointer[0]);
int size = file.tellg();
PrintOctets16(cPointer);
return 0;
}
ok with the printf function is it like this:
011010111000101101000101011001110011001001111011001000111100011...
10101110 01011100
10111000 01110001
11100010 11000101
10001011 00010110
00101101 01011010
10110100 01101000
11010001 10100010
and with no printf is it like:
01101011 11010111
10101110 01011100
10111000 01110001
11100010 11000101
10001011 00010110
00101101 01011010
10110100 01101000
11010001 10100010
01000101 10001010
this are only parts of the output.
but the data.dat starts like:
0110101110001011010001010110011100110010011110110010001111
Last edited on Oct 25, 2016 at 5:13pm Oct 25, 2016 at 5:13pm UTC
Oct 25, 2016 at 5:36pm Oct 25, 2016 at 5:36pm UTC
Thank u very much, this problem is solved, but uf is with the
printf("%s" ,&cPointer[0]);
expression, why does sshe changes the outcome?
Oct 25, 2016 at 5:49pm Oct 25, 2016 at 5:49pm UTC
%s tells printf that you are supplying it with a C-style nul-terminated string. You are not supplying it with one of those, so don't tell it you are.