Help

This is a discussion on Help within the C Programming forums, part of the General Programming Boards category; Hi i have a question: how can we wirte a program that can print the 2 first bytes of a ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Help

    Hi
    i have a question:
    how can we wirte a program that can print the 2 first bytes of a double ??

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Can you clearify that, maybe give an example. And then try it on your own so we have some code work with. No one wants to write the program for you.
    Don't quote me on that... ...seriously

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    I don't want anyone to write a program for me !
    i just needed help thats all !

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    11
    What about Mod division or something similar?

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can cast the field as something else to print the first 2 bytes, either with a (cast) operator or perhaps code up a union to redefine it.

    Todd

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Depends what you mean by the first two bytes. Doubles are generally not guarenteed to be kept in the same binary format that an integer is. You also run into issues with endianness.

    If you don't care about any of that and just want to print it for the heck of it, then Todd's suggestion of a union is probably a good idea. The same can manually be done without a union, but a union takes care of some pointer and casting details.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The easiest idea I find (if you don't need to worry about endianess) is to cast it to char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Here's something with the union idea:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef union
    {
    	char	asChar;
    	short	asShort;
    	int	asInt;
    	long	asLong;
    	double	asDouble;
    }doublewrapper;
    
    void dumpWrapper(const doublewrapper * const dw);
    
    int main(void)
    {
    	doublewrapper dw;
    	memset(&dw, 0, sizeof(doublewrapper));
    
    	printf("Enter a double: ");
    	fflush(stdout);
    
    	scanf("%lf", &dw.asDouble);
    
    	dumpWrapper(&dw);
    
    	return 0;
    }
    
    void dumpWrapper(const doublewrapper * const dw)
    {
    	printf("Char\t\tShort\t\tInt\t\tLong\t\tDouble\n\n");
    	printf("%08d\t%08d\t%08d\t%08ld\t%f\n", dw->asChar, dw->asShort, dw->asInt, dw->asLong, dw->asDouble);
    	printf("%08X\t%08X\t%08X\t%08lX\t%f\n", dw->asChar, dw->asShort, dw->asInt, dw->asLong, dw->asDouble);
    }
    Disclaimer: This is very messy, non-portable (in the sense that you may receive different numerical results on different systems), and possibly has a few errors in it (since I rushed it).

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Code:
    int main()
    {
    	int i = 0;
    	double myd = 1234.0;
    	char* p = (char*)&myd;
    	for (; i < sizeof(myd); i++, p++)
    	{
    		printf("%i", (int)*p);
    	}
    	return 0;
    }
    Should do the same thing, I believe. Not tested, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It might be a good idea to use unsigned chars, because ordinary chars can be signed, and the values dealt with might easily be negative when represented as signed chars. Unless you don't mind negative byte values, in which case you might want to use signed chars specifically, because ordinary chars can be signed or unsigned.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    bit masking could do as well i suppose

    ssharish

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    thanks Guys!

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And of course without knowing at least the byteorder of the architecture, you have no idea if you are displaying the least significant digits or the exponent and sign bytes of the double.

    The whole excercise is pretty meaningless unless it's for the purpose of understanding unions/pointer casts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21