# Thread: translate int_32 to 8-bit

1. ## translate int_32 to 8-bit

hello,
So, if I converting ip address from 8-bit to int_32 type.
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. 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. If I doing whatever like this
Code:
```for (address=first_address; address<first_address+hosts; ++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

4. Could you post the code of how you populate the variable that holds the ip. Also, post what the 32bit ip is, untouched.

5. 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

6. Originally Posted by vart
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. Could you help me?

8. Are you resetting i in the second for loop, now?

9. Have you updated the code after incorporating suggestions made by the other posters?

Popular pages Recent additions