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:
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)
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)
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)