Jump to content


Photo

Programming Question


  • Please log in to reply
6 replies to this topic

#1 linuxuser

linuxuser

    Honourable Member

  • WNSO Addict
  • PipPipPipPip
  • 1,147 posts

Posted 25 February 2003 - 03:02 AM

I have a small programming question. It is more like C/C++ programming question. I am trying to write a function in C or C++ that allows me to write a 9 bit value to a file. Can anyone help me with this. Psuedocode or code will be helpful. smile.gif

:)

#2 sumanghimire

sumanghimire

    WNSO Ireland Co-ordinator

  • Members
  • PipPipPip
  • 227 posts

Posted 27 February 2003 - 01:00 PM

you can find useful links in the web.go to google.
Best Regards,
Suman Ghimire
WNSO Ireland Co-ordinator
WNSO Ireland
www.ireland.wnso.org

#3 sanskar

sanskar

    Honourable Member

  • Members
  • PipPipPipPip
  • 790 posts

Posted 27 February 2003 - 01:02 PM

QUOTE (rs_1915 @ Feb 25 2003, 04:02 AM)
I have a small programming question. It is more like C/C++ programming question. I am trying to write a function in C or C++ that allows me to write a 9 bit value to a file. Can anyone help me with this. Psuedocode or code will be helpful. smile.gif

Dear rs_1915,

Can you elaborate little bit more your problem. just saying to write 9 bit value to file is not much clear. It would be easy to understand, if you can give some example. like input and output.

regards,
Sanskar Shrestha
________________________________________
Learn as if you were going to live forever.
Live as if you were going to die tomorrow.

#4 linuxuser

linuxuser

    Honourable Member

  • WNSO Addict
  • PipPipPipPip
  • 1,147 posts

Posted 27 February 2003 - 02:13 PM

Thanks guys for the reply.
Sanskar..let me try to explain the problem.
In C/C++ , u can either write a byte ( which is char = 1 byte or 8 bits), or u can write 2 bytes ( word), or u can write int ( which is 4 bytes) in x86 machines.
u can use putc( char, FILE) and write a char variable ( again it is 1 byte or 8 bits), or use fwrite( buffer, sizeof( unsigned int) , 1, FILE) and write 1 byte, 2 bytes, or 4 bytes.
but there is not any function in ANSI C/C++ which can write 9 bits...........8 bits + 1 bit.
so my question is how do u write it?
smile.gif
:)

#5 sanskar

sanskar

    Honourable Member

  • Members
  • PipPipPipPip
  • 790 posts

Posted 27 February 2003 - 02:25 PM

ok rs_1915,

I think, I understood your problem now. Do you know how to write single bit in file? You have to use memory function. Please follow this link for detail.

http://www.experts-e...Q_20394965.html

hope this helps

regards,
Sanskar Shrestha
________________________________________
Learn as if you were going to live forever.
Live as if you were going to die tomorrow.

#6 linuxuser

linuxuser

    Honourable Member

  • WNSO Addict
  • PipPipPipPip
  • 1,147 posts

Posted 28 February 2003 - 02:06 PM

Sanskar,,,thanks for the help!!
I found the solution and decided to post it here. ( may be somebody in future is looking for similar type of solution).
I also encourage all the nepalese students studying computer science to post their problems if they have any ( software or hardware).

Ok in theory, there are two solutions.
1)we know that we can write1 byte or 2 bytes or 4 bytes. since 9 bits value is has 1 bit more than a byte( 8 bits), so try to convert 9 bits value to word( 2 bytes) or int( 4 bytes). the way of converting is, we can just pad 0 byte at the end of 9 bits and make it 2 bytes of 4 bytes
eg. 10101010 1 ( 9 bits value)
becomes 10101010 10000000 ( 2 bytes).
2) Another solution (better one) if u want to pack is..
write only 8 bits out of 9 bits...save 1 bit and add that 1 bit with next 9 bits. now again write 8 bits from this new 9 bits value,,,now u have 2 bits extra..add that with other new 9 bits...and so on.
code for this is here...

output(FILE *file,unsigned int value)
{
static int bit_count=0;
static unsigned long bit_buffer=0L;
bit_buffer |= (unsigned long) value << (32-9-bit_count);
bit_count += 9;
while (bit_count >= 8)
{
putc(bit_buffer >> 24,file);
bit_buffer <<= 8;
bit_count -= 8;
}
}


:)

#7 sanskar

sanskar

    Honourable Member

  • Members
  • PipPipPipPip
  • 790 posts

Posted 28 February 2003 - 02:09 PM

thanks rs_1915 for code and explanation. hope it will be helpful to other also. if you have any interesting problem/solution, then please do share here. hope other will also do same.

best regards,
Sanskar Shrestha
________________________________________
Learn as if you were going to live forever.
Live as if you were going to die tomorrow.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users