1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

reading bytes to a variable....

Discussion in 'C' started by ceus, Oct 16, 2006.

  1. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    read them into 2 char and then combine them using sprintf.
     
  3. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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...
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    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
     
  5. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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 ??
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Thats why I mentioned convert them to int.
     
  7. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    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 ???
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  9. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    thanks shabbir...
    but if the value is 3A i have to read that as 16*3+10=58..... how can i do this....
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
  11. ceus

    ceus New Member

    Joined:
    Oct 16, 2006
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    0
    oh... fine codes... thanks shabbir... i will come back if i have some moere doubts...
    thanks again.... :) :)
     

Share This Page