LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C, read 8 bit octal numbers, convert to 24 bit binary (https://www.linuxquestions.org/questions/programming-9/c-read-8-bit-octal-numbers-convert-to-24-bit-binary-136672/)

bamalabs 01-20-2004 08:03 AM

C, read 8 bit octal numbers, convert to 24 bit binary
 
I'm need to read a file containing 8 columns of 8 bit octal numbers, and convert to 24 bit binary string. No experience here. Do I have to convert from octal to decimal, then decimal to binary? If so, any hints as to how to do this? Any help appreciated.

jtshaw 01-20-2004 09:32 AM

strtol will take a string with a number in it and convert it to a long int. You would set the base argument to 8 in your case. It's function prototype looks as follows:

#include <stdlib.h>

long int strtol(const char *nptr, char **endptr, int base);

Reading the man page on this (man strtol) will tell you more. The manual pages are also available on-line at numerous different places.

As far as converting it to binary, I'll leave that to you, this sounds suspiciously like a school project:)

bamalabs 01-20-2004 09:55 AM

Nope, not a school project. Actually a work project. I think I can handle the conversion to binary, though. I hadn't heard of strtol before, so thanks for your help.

jtshaw 01-20-2004 09:59 AM

Ya, I haven't seen a built in function that does it, but shoudl be easy to do some moding in a for loop and build a string with the binary.

Glad I could offer some assistance.


All times are GMT -5. The time now is 11:21 PM.