Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   reading bytes to a variable.... (http://www.go4expert.com/forums/reading-bytes-variable-t1644/)

ceus 16Oct2006 20:56

reading bytes to a variable....
 
Hi all,
i am new to this forum, and i am a C beginner... can anyone tell me that how to read 2 bytes of data from a file and have that in a single variable ???
thank u...

shabbir 17Oct2006 11:01

Re: reading bytes to a variable....
 
read them into 2 char and then combine them using sprintf.

ceus 18Oct2006 14:29

Re: reading bytes to a variable....
 
thanks for the reply shabbir,
i will tell my problem more specifically.... i have a hex file and i wanted to read 2nd and 3rd bytes of that file which contains some information about some size... i want that size to be extracted in a single variable.....please help me
thanks...

shabbir 18Oct2006 15:09

Re: reading bytes to a variable....
 
Read second byte into a char say an array of 2 elements x[0] and 3rd byte into char x[1].

Convert them to int and then combine both the bytes using atoi function

ceus 24Oct2006 11:38

Re: reading bytes to a variable....
 
thanks shabbir,
i tried that... but by using atoi only integers will be displayed...what if the values stored are in HEX say...A,B....F... atoi function will not recognize this ??? am i right ??

shabbir 25Oct2006 10:02

Re: reading bytes to a variable....
 
Thats why I mentioned convert them to int.

ceus 25Oct2006 11:23

Re: reading bytes to a variable....
 
sorry i didnt get u ???
say i have 3 and A(hex value) in the second and third locations of the file... by using atoi it only recognizes it as 3 ......and A is truncated right ???

shabbir 25Oct2006 12:33

Re: reading bytes to a variable....
 
You are reading 3 now convert it to integer. which will be 3.

Now read A, Convert it to int which will be 10

Now combine them using atoi function.

Probably I wasnt clear in my previous post but I think this should.

ceus 25Oct2006 12:55

Re: reading bytes to a variable....
 
thanks shabbir...
but if the value is 3A i have to read that as 16*3+10=58..... how can i do this....

shabbir 25Oct2006 13:47

Re: reading bytes to a variable....
 
I guess it should go like read the string as Hex like 3A. Now convert the 3A using the Decimal, Hex, octal and binary number inter conversion.

3A in Hex is equivalent to 58 in Decimal and so you dont need to go about reading the bytes and then converting.


All times are GMT +5.5. The time now is 09:33.