Thread: translate int_32 to 8-bit

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    translate int_32 to 8-bit

    hello,
    So, if I converting ip address from 8-bit to int_32 type.
    address = (((((ip[0]<<8)+ip[1])<<8)+ip[2])<<8)+ip[3];
    But, which's way reverse that?
    I tried this way
    Code:
    for (;i<4; ++i) id[i] = (address >> (i*8)) & 0xFF;
    printf("%u\n", id[i]);
    However. I've got a value prefix where has been that ip-address.
    How to do converting value from int_32 to 8-bit?

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    It seems you are doing it correct.
    Lets say this ip is stored like this: 0x01020304
    Code:
    ip >> 0 = 0x04
    ip >> 8 = 0x03
    ip >> 16 = 0x02
    ip >> 32 = 0x01
    So you could do something along the lines of this:
    Code:
    char temp;
    
        for (i = 0; i < 4; i++)
            temp = (ip >> (8 * i)) & 0xFF;

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    If I doing whatever like this
    Code:
    for (address=first_address; address<first_address+hosts; ++address){
    printf("%u\n\n", address);
    for (;i<3; i++) id[i] = (address >> (i*8)) & 0xFF;
    printf("%u\n", id[i]);
    }
    I've got it crazy of result
    Code:
    3232240640
    
    0
    3232240641
    
    0
    3232240642
    
    0
    3232240643
    
    0
    But I expect something like this
    3232240640
    192.168.20.0
    3232240641
    192.168.20.1
    3232240642
    192.168.20.2
    3232240643
    192.168.20.3
    Last edited by quantt; 01-25-2009 at 01:41 AM.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Could you post the code of how you populate the variable that holds the ip. Also, post what the 32bit ip is, untouched.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for (;i<3; i++) id[i] = (address >> (i*8)) & 0xFF;
    printf("%u\n", id[i]);
    you ar printing id[4] - which is out of bounds access

    as well as your i is never reset to 0 on the next iteration of the outher loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    you ar printing id[4] - which is out of bounds access
    as well as your i is never reset to 0 on the next iteration of the outher loop
    ops, I gaming with it and forgot change it before posted.
    Could you post the code of how you populate the variable that holds the ip. Also, post what the 32bit ip is, untouched.
    sure
    Code:
    sscanf(argv[1], "%d.%d.%d.%d/%d", &ip[0], &ip[1], &ip[2], &ip[3], &mask);
    degree = 32 - mask;
    hosts=1 << degree;

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    Could you help me?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you resetting i in the second for loop, now?

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Have you updated the code after incorporating suggestions made by the other posters?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. bit conversions
    By wazilian in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:59 PM