Thursday 15 May 2014

bash - Binary to ASCII different between C++ and Grep? -



bash - Binary to ASCII different between C++ and Grep? -

i'm trying figure out how agument recorded in compiled binary file of c/c++ program. next program. i'm trying create simple possible

void f(char a,char b){} int main(){f(12,23);}

in order able "read" binary file, need convert ascii "representable" form. find out that

grep $'\xx' a.out

actually works a.out binary file , xx decimal ascii code. grep can't tell me since output "binary match". , if forcefulness print out '-a', print out everything. though, can utilize -c alternative see how many of them there:

grep $'\12' b.out (i renamed file) ==> 4 grep $'\23' b.out ==> 3

but in order study something, need exact position. programmed programme prints out ascii accroding char.

#include<iostream> using namespace std; int main(){char c; while(cin>>c)cout<<(int)c<<' ';}

but when run next command, result don't match:

./a.out<./b.out|tr ' ' '\n'|grep -c '^12$' ==> 0 ./a.out<./b.out|tr ' ' '\n'|grep -c '^23$' ==> 4

i'm wondering did write wrong in test program? or grep has kind of special mecanism (like not byte-by-byte)? , 1 correct? or can straight provide me reply to: how "1,2,3,4" in func(1,2,3,4) recorded in binary

edt1 advise, used "od -tu1" replace test programme works good. , enhanced tested programme little bit argument more obvious , numbers won't "disappear":

void f(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j,int k,int l,int m,int n,int o,int p,int q,int r,int s,int t){a+=b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t;} int main(){f(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);}

by changing arguments , using "diff" command, find out position of these numbers in binary:

0002560 68 36 104 19 0 0 0 199 68 36 96 18 0 0 0 199 0002600 68 36 88 17 0 0 0 199 68 36 80 16 0 0 0 199 0002620 68 36 72 15 0 0 0 199 68 36 64 14 0 0 0 199 0002640 68 36 56 13 0 0 0 199 68 36 48 12 0 0 0 199 0002660 68 36 40 11 0 0 0 199 68 36 32 10 0 0 0 199 0002700 68 36 24 9 0 0 0 199 68 36 16 8 0 0 0 199 0002720 68 36 8 7 0 0 0 199 4 36 6 0 0 0 65 185 0002740 5 0 0 0 65 184 4 0 0 0 185 3 0 0 0 186 0002760 2 0 0 0 190 1 0 0 0 191 0 0 0 0 232 234

as can see, 19~9 written here. since 8, 0, things start changing in not understandable way.. displacement between digits becoming smaller. , don't understand what's number between them (i understand 0 "int" part (little endian?)). numbers represent kind of address "plug-in"? different according different position , lengh different?

wow. question shows you're willing experiment , eager learn, there's lot more understand happens in stack overflow question.

first, grep powerful tool, not appropriate task. you'll much more interested in od give raw binary dump of file. (look flags see how output hexidecimal, decimal, or pure binary.)

next, if want write binary file, you're going have mess of stuff through if write in executable. variables you're storing, executable have of code you're compiling. hard isolate (presumably) 4 bytes represent variables, , you'll want read lot on format behind a.out executable able it.

it much cleaner write c programme write binary file, i.e. like:

#include <stdio.h> int main() { int one; int two; int three; int four; 1 = 1; 2 = 2; 3 = 3; 4 = 4; file* fp = fopen("test.dat", "wb"); fwrite(&one, sizeof(int), 1, fp); fwrite(&two, sizeof(int), 1, fp); fwrite(&three, sizeof(int), 1, fp); fwrite(&four, sizeof(int), 1, fp); fclose(fp); homecoming 0; }

there tons of other ways write same code, , folks can right glaring mistakes made (it's been while since i've coded c without compiler), should write 4 integers.

finally, quick reply question. assuming int 32 bits, you'll writing these numbers in binary. you'll have "big-endian vs. little-endian" understand next part, depending on architecture, you'll 1 or other. big-endian more intuitive, i'll reply using concept.

numbers stored 32 bit binary values. (the first bit in int sign bit. if it's 1, value negative, , you'll have "two's complement" understand notation.) in case, "1, 2, 3, 4", lastly 3 bits matter, you'll see lot of 0s:

1: 00000000 0000000 00000000 00000001 2: 00000000 0000000 00000000 00000010 3: 00000000 0000000 00000000 00000011 4: 00000000 0000000 00000000 00000100

note, gets clunky, tend utilize hexadecimal. using that, can represent each 8-bit byte in 2 characters. in hex, reply be:

1: 00 00 00 01 2: 00 00 00 02 3: 00 00 00 03 4: 00 00 00 04 17: 00 00 00 11 255: 00 00 00 ff

you've got lot of learning do, maintain up! think it's wonderful how eager experiment. hope helps.

c++ bash grep bin

No comments:

Post a Comment