As long as you are converting unsigned integral values that fit into an integral type supported by your compiler, using something similar to what migurus suggested in post #2 in this thread will be much faster. If you are trying to convert arbitrarily large values, bc is your friend. For example, suppose we have the bc script:
If we directly invoke bc using:
to start bc with its math library and the above file loaded, it will then read commands from standard input. So we can have an interactive session as shown here (this is with the bc utility included with OS X El Capitan Release 10.11.3):
(where the text shown in bold was typed by me and the other text was produced by bc). The Free Software Foundation copyright notice is only displayed when stdout is connected to a terminal device file (so if you pipe this output into another process, you don't have to worry about skipping over that text. Not that "long" numbers are split by bc into multiple lines when then are printed with all but the last line terminated by a <backslash> immediately preceding the <newline> line terminator. (Note also that the f(bits, value) function in the above file does not make any attempt to split long strings of leading zeros, so if you print a small value with a large number of bits requested, you can get much longer lines.
If you just wanted to print various numbers using the bc f() function defined in the above file, you could write a C program using system() or popen() to invoke bc with the above script. But, if you want a C subroutine to be able to send commands interactively to bc running the above script and read the responses the script produces, you could use something like:
Since we are writing data to bc through a pipe and reading data from bc from a pipe, popen() and system() won't really do what we want. So this uses fork() and execlp() to start bc and uses pipe(), close(), and dup(), and fdopen() to connect two pipes between the parent and child connecting the read end of one pipe to the standard input of bc and the write end of the other pipe to the standard output and standard error output of bc. When the parent reads data from bc it throws away <backslash><newline> character pairs while copying the results to the parent's standard output. Note that you'll have to change the pathname defined by the macro BINARY_PRINT_PATH to specify an absolute pathname specifying where you placed the bc script above on your system. If you save the above C code in a file named tester.c and build it using:
and invoke it with something like:
producing the output (as viewed on an 80 column wide terminal with line wrapping enabled [to avoid 240 character wide displays in this post]):
I assume that instead of printing the binary values, you'll want to set up an internal array containing bits or bytes containing the values reported by bc, but I hope you'll be able to use this as a guide for what needs to be done to communicate with a child process and show you how you can modify string output from one process to get a desired output format.
Last edited by Don Cragun; 03-10-2016 at 06:13 AM..
Reason: Fix typos.
This User Gave Thanks to Don Cragun For This Post:
Thanks Don!
Your reply overwhelmed me to death! As usual, this is way more than I needed.
BTW, how long does it take for a regular CS graduate to get the level like this with C?
Thanks Don!
Your reply overwhelmed me to death! As usual, this is way more than I needed.
BTW, how long does it take for a regular CS graduate to get the level like this with C?
I'm glad that my code helped, but there isn't anything overwhelming in what I did. Start at the beginning and decide what you want to do. Then build the pieces you need to get there one by one until you have something that works. If you run into a problem with one of the pieces fitting in after you have started working on another piece, don't be afraid to throw one or both of them away and redo it/them a different way that fits in better.
I have no idea what a regular CS graduate now days knows about C, nor how long it takes one to be able to read, understand, nor write a program like this. But, a few decades of fixing bugs in other people's code, writing my own code, and writing and maintaining the standards that specify the behavior of the POSIX-specified utilities and C interfaces certainly helps. The key point is that practicing programming helps.
When I was a CS grad, I knew FORTRAN and three different assembler languages and a couple of FORTRAN high precision math libraries. I picked up Algol, COBOL, Lisp, and PL/1 in a comparative programming languages course one semester while working on my Masters in computer engineering. (I used to have decks of cards in Algol, COBOL, Fortran, Lisp, and PL/1 that could read in a number expressed as Roman numerals, multiply it by two and add seven, and write out the result as Roman numerals. I won't say that I mastered COBOL, Lisp, and PL/1 in those thirteen weeks; but I learned how to read a manual and how to experiment with code.) After college, I was able to learn a few other programming languages in about a week of concentrated study as long as I had easy access to a compiler for that language, a good manual for that language, and no distractions. Note, however, that I have never claimed to be normal nor regular.
This User Gave Thanks to Don Cragun For This Post:
Hello everyone.
I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
I have an assignment in which a character is the input of which some bits(from a position to certain position) are to be inverted (1's complement) and then the resultant character is to be returned....for example
unsigned char x = J from p = 3 to offset n = 5
01001010 inverted to... (1 Reply)
Hi,
Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result.
perl interpreter info,
perl, v5.8.8 built for... (0 Replies)