Sponsored Content
Homework and Emergencies Homework & Coursework Questions Splitting a numbers binary representation for a double. Post 302664751 by agama on Saturday 30th of June 2012 01:56:49 PM
Old 06-30-2012
It would be nice to know what you think isn't working, or where you are stuck. Modulo that, from looking at (not trying to build/execute) your code....

Your approach makes a mountain from a mole hill I think; unless you aren't allowed to use strtoll(). If there are no restrictions, then have a look at the strtoll() manual page, and if you need to add error checking to ensure that a non-hex character hasn't been entered, have a peek at the strspn() manual page too.

If there are restrictions, there still isn't the need for the use of the pow() function; simple (unsigned) character based math and shifting are all that are needed. Consider this example:

Code:
   unsigned char *cval = "123";
    int val = 0;
    int i;

    for( i = 0; i < strlen( cval ); i++ )
    {
        val <<= 4;
        val += cval[i] - '0';
    }
    printf( "%x\n", val );

You'll have to handle a-f and A-F which aren't handled here, but this might point you in the direction of an easier way if strtoll() has to be avoided for the assignment.

Using floating point values for this will make things much more difficult; stick with 64 bit integer. I'd also think about bitwise AND and shift operations as a way to split your key.
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using binary representation of IEEE 754

Hi! What is the way to get the binary representation of a IEEE 754 (like 0001110110001111010101100111001011100100101010111101) without using perl. Bash for example, would be fine. I need to put that representation in a string for some operation and then to put that string back in a float. ... (3 Replies)
Discussion started by: Zephyr
3 Replies

2. Programming

Internal representation of double

I came across a puzzle which I can not explain. The setup is SCO OpenServer 5.7 (32 bit OS) and native SCO compiler. double is 8 bytes long on this system. I am able to populate the double variable with two different sets of values that produces the same double value, please see below: #include... (7 Replies)
Discussion started by: migurus
7 Replies

3. Shell Programming and Scripting

Splitting file based on line numbers

Hello friends, Is there any way to split file from n to n+6 into 1 file and (n+7) to (n+16) into other file etc. f.e I have source pipe delimated file with 20 lines and i need to split 1-6 in file1 and 7-16 in file2 and 17-20 in file 3 I need to split into fixed number of file like 4 files... (2 Replies)
Discussion started by: Rizzu155
2 Replies

4. Shell Programming and Scripting

Put double quotes around numbers

Hi, consider a file which has data such as "random text",912345,"54","finish" "random text",9991236745,"9954","finish" I want to replace the numbers that don't have double quotes around them with ones that do; so the output should be "random text","912345","54","finish" "random... (4 Replies)
Discussion started by: Storms
4 Replies

5. Shell Programming and Scripting

Splitting a file based on positive and negative numbers

Dear All, I have to split a tab delimited file in two files based on the presence of a positive or negative in column number 9 , for example file: A 1 5 erg + 6766 0.9889 0.9817 9.01882 erg inside upstream B 1 8 erg2 + 6766 0.9889 0.9817 -9.22 erg2 inside... (3 Replies)
Discussion started by: paolo.kunder
3 Replies

6. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

7. Programming

How bit representation of a number can be done in c?

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)
Discussion started by: ezee
1 Replies

8. UNIX for Beginners Questions & Answers

Splitting a file based on negative and positive numbers

I have a file that is pipe delimited and in Column F they have number values, both positive and negative. I need to take the one file I am starting with and split it into two separate files based on negative and positive numbers. What is the command to do so? And then I need to also transfer... (4 Replies)
Discussion started by: cckaiser15
4 Replies
STRTOL(3)						     Linux Programmer's Manual							 STRTOL(3)

NAME
strtol, strtoll, strtoq - convert a string to a long integer SYNOPSIS
#include <stdlib.h> long int strtol(const char *nptr, char **endptr, int base); long long int strtoll(const char *nptr, char **endptr, int base); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): strtoll(): XOPEN_SOURCE >= 600 || _BSD_SOURCE || _SVID_SOURCE || _ISOC99_SOURCE; or cc -std=c99 DESCRIPTION
The strtol() function converts the initial part of the string in nptr to a long integer value according to the given base, which must be between 2 and 36 inclusive, or be the special value 0. The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal). The remainder of the string is converted to a long int value in the obvious manner, stopping at the first character which is not a valid digit in the given base. (In bases above 10, the letter 'A' in either upper or lower case represents 10, 'B' represents 11, and so forth, with 'Z' representing 35.) If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '' but **endptr is '' on return, the entire string is valid. The strtoll() function works just like the strtol() function but returns a long long integer value. RETURN VALUE
The strtol() function returns the result of the conversion, unless the value would underflow or overflow. If an underflow occurs, strtol() returns LONG_MIN. If an overflow occurs, strtol() returns LONG_MAX. In both cases, errno is set to ERANGE. Precisely the same holds for strtoll() (with LLONG_MIN and LLONG_MAX instead of LONG_MIN and LONG_MAX). ERRORS
EINVAL (not in C99) The given base contains an unsupported value. ERANGE The resulting value was out of range. The implementation may also set errno to EINVAL in case no conversion was performed (no digits seen, and 0 returned). CONFORMING TO
strtol() conforms to SVr4, 4.3BSD, C89, C99 and POSIX.1-2001, and strtoll() to C99 and POSIX.1-2001. NOTES
Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure, the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a nonzero value after the call. In locales other than the "C" locale, other strings may also be accepted. (For example, the thousands separator of the current locale may be supported.) BSD also has quad_t strtoq(const char *nptr, char **endptr, int base); with completely analogous definition. Depending on the wordsize of the current architecture, this may be equivalent to strtoll() or to strtol(). EXAMPLE
The program shown below demonstrates the use of strtol(). The first command-line argument specifies a string from which strtol() should parse a number. The second (optional) argument specifies the base to be used for the conversion. (This argument is converted to numeric form using atoi(3), a function that performs no error checking and has a simpler interface than strtol().) Some examples of the results produced by this program are the following: $ ./a.out 123 strtol() returned 123 $ ./a.out ' 123' strtol() returned 123 $ ./a.out 123abc strtol() returned 123 Further characters after number: abc $ ./a.out 123abc 55 strtol: Invalid argument $ ./a.out '' No digits were found $ ./a.out 4000000000 strtol: Numerical result out of range Program source #include <stdlib.h> #include <limits.h> #include <stdio.h> #include <errno.h> int main(int argc, char *argv[]) { int base; char *endptr, *str; long val; if (argc < 2) { fprintf(stderr, "Usage: %s str [base] ", argv[0]); exit(EXIT_FAILURE); } str = argv[1]; base = (argc > 2) ? atoi(argv[2]) : 10; errno = 0; /* To distinguish success/failure after call */ val = strtol(str, &endptr, base); /* Check for various possible errors */ if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) { perror("strtol"); exit(EXIT_FAILURE); } if (endptr == str) { fprintf(stderr, "No digits were found "); exit(EXIT_FAILURE); } /* If we got here, strtol() successfully parsed a number */ printf("strtol() returned %ld ", val); if (*endptr != '') /* Not necessarily an error... */ printf("Further characters after number: %s ", endptr); exit(EXIT_SUCCESS); } SEE ALSO
atof(3), atoi(3), atol(3), strtod(3), strtoul(3) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2007-07-26 STRTOL(3)
All times are GMT -4. The time now is 08:52 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy