memset char * with Integer - c
I have to memset 4 bytes in a char * with an integer.
For exemple I have an integer int i = 3276854 (0x00320036) and a char header[54] = {0}. I have to write i at header + 2, as if I print memory from header[2] to header[6], I get :
0032 0036
I tried this:
memset(&header[2], i, 1);
But it seems to put only the last byte into header, I get :
0036 0000
I also tried:
memset(&header[2], i, 4);
But it fill each byte with the last byte of i, I get :
3636 3636
I also tried to use binary masks like that :
ft_memset(&header[2], (int)(54 + size) & 0xff000000, 1);
ft_memset(&header[3], (int)(54 + size) & 0x00ff0000, 1);
ft_memset(&header[4], (int)(54 + size) & 0x0000ff00, 1);
ft_memset(&header[5], (int)(54 + size) & 0x000000ff, 1);
I get :
3600 0000.
So I don't know how I can get my 0032 0036, or at least 3600 3200 (maybe there is a thing with little and big endian into that, because I run it under MacOS, which is big endian).
memset fills memory with a constant byte value. The second parameter (of type int) is converted to an unsigned char value.
You could use memcpy like this:
memcpy(&header[2], &i, sizeof(i));
However, it depends what exactly you are trying to achieve. If the header needs the integer to be in a particular format, you may need to convert the value in some way. For example, if the value needs to be big-endian (which is also known as "network byte order" in several Internet protocols), you can convert it with the htonl function:
uint32_t bi = htonl(i);
memcpy(&header[2], &bi, sizeof(bi));
(The htonl function is defined by #include <arpa/inet.h>.)
Also check the newer byte order conversion functions htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, and le64toh declared by:
#define _BSD_SOURCE
#include <endian.h>
These convert between host byte order and little-endian byte order, or between host byte order and big-endian byte order, and work on uint16_t, uint32_t or uint64_t values, depending on the function name.
If there are no equivalents to those byte-order conversion functions provided on your system the following non-optimized, but portable (on implementations that support uint16_t, uint32_t and uint64_t) functions may be used:
myendian.h
#ifndef MYENDIAN_H__INCLUDED_
#define MYENDIAN_H__INCLUDED_
#include <stdint.h>
uint16_t my_htobe16(uint16_t h16);
uint16_t my_htole16(uint16_t h16);
uint16_t my_be16toh(uint16_t be16);
uint16_t my_le16toh(uint16_t le16);
uint32_t my_htobe32(uint32_t h32);
uint32_t my_htole32(uint32_t h32);
uint32_t my_be32toh(uint32_t be32);
uint32_t my_le32toh(uint32_t le32);
uint64_t my_htobe64(uint64_t h64);
uint64_t my_htole64(uint64_t h64);
uint64_t my_be64toh(uint64_t be64);
uint64_t my_le64toh(uint64_t le64);
#endif
myendian.c
#include "myendian.h"
union swab16
{
uint16_t v;
uint8_t b[2];
};
union swab32
{
uint32_t v;
uint8_t b[4];
};
union swab64
{
uint64_t v;
uint8_t b[8];
};
static uint16_t xbe16(uint16_t x)
{
union swab16 s;
s.b[0] = (x >> 8) & 0xffu;
s.b[1] = x & 0xffu;
return s.v;
}
static uint16_t xle16(uint16_t x)
{
union swab16 s;
s.b[0] = x & 0xffu;
s.b[1] = (x >> 8) & 0xffu;
return s.v;
}
static uint32_t xbe32(uint32_t x)
{
union swab32 s;
s.b[0] = (x >> 24) & 0xffu;
s.b[1] = (x >> 16) & 0xffu;
s.b[2] = (x >> 8) & 0xffu;
s.b[3] = x & 0xffu;
return s.v;
}
static uint32_t xle32(uint32_t x)
{
union swab32 s;
s.b[0] = x & 0xffu;
s.b[1] = (x >> 8) & 0xffu;
s.b[2] = (x >> 16) & 0xffu;
s.b[3] = (x >> 24) & 0xffu;
return s.v;
}
static uint64_t xbe64(uint64_t x)
{
union swab64 s;
s.b[0] = (x >> 56) & 0xffu;
s.b[1] = (x >> 48) & 0xffu;
s.b[2] = (x >> 40) & 0xffu;
s.b[3] = (x >> 32) & 0xffu;
s.b[4] = (x >> 24) & 0xffu;
s.b[5] = (x >> 16) & 0xffu;
s.b[6] = (x >> 8) & 0xffu;
s.b[7] = x & 0xffu;
return s.v;
}
static uint64_t xle64(uint64_t x)
{
union swab64 s;
s.b[0] = x & 0xffu;
s.b[1] = (x >> 8) & 0xffu;
s.b[2] = (x >> 16) & 0xffu;
s.b[3] = (x >> 24) & 0xffu;
s.b[4] = (x >> 32) & 0xffu;
s.b[5] = (x >> 40) & 0xffu;
s.b[6] = (x >> 48) & 0xffu;
s.b[7] = (x >> 56) & 0xffu;
return s.v;
}
uint16_t my_htobe16(uint16_t h16)
{
return xbe16(h16);
}
uint16_t my_htole16(uint16_t h16)
{
return xle16(h16);
}
uint16_t my_be16toh(uint16_t be16)
{
return xbe16(be16);
}
uint16_t my_le16toh(uint16_t le16)
{
return xle16(le16);
}
uint32_t my_htobe32(uint32_t h32)
{
return xbe32(h32);
}
uint32_t my_htole32(uint32_t h32)
{
return xle32(h32);
}
uint32_t my_be32toh(uint32_t be32)
{
return xbe32(be32);
}
uint32_t my_le32toh(uint32_t le32)
{
return xle32(le32);
}
uint64_t my_htobe64(uint64_t h64)
{
return xbe64(h64);
}
uint64_t my_htole64(uint64_t h64)
{
return xle64(h64);
}
uint64_t my_be64toh(uint64_t be64)
{
return xbe64(be64);
}
uint64_t my_le64toh(uint64_t le64)
{
return xle64(le64);
}
Test harness: myendiantest.c
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "myendian.h"
#define TEST(n, fn, v) \
printf("%s(%#" PRIx##n ") = %#" PRIx##n "\n", #fn, (v), (fn)(v))
int main(void)
{
const uint16_t t16 = UINT16_C(0x1234);
const uint32_t t32 = UINT32_C(0x12345678);
const uint64_t t64 = UINT64_C(0x123456789abcdef);
TEST(16, my_htobe16, t16);
TEST(16, my_htole16, t16);
TEST(16, my_be16toh, t16);
TEST(16, my_le16toh, t16);
TEST(32, my_htobe32, t32);
TEST(32, my_htole32, t32);
TEST(32, my_be32toh, t32);
TEST(32, my_le32toh, t32);
TEST(64, my_htobe64, t64);
TEST(64, my_htole64, t64);
TEST(64, my_be64toh, t64);
TEST(64, my_le64toh, t64);
return 0;
}
Output on a little endian system:
my_htobe16(0x1234) = 0x3412
my_htole16(0x1234) = 0x1234
my_be16toh(0x1234) = 0x3412
my_le16toh(0x1234) = 0x1234
my_htobe32(0x12345678) = 0x78563412
my_htole32(0x12345678) = 0x12345678
my_be32toh(0x12345678) = 0x78563412
my_le32toh(0x12345678) = 0x12345678
my_htobe64(0x123456789abcdef) = 0xefcdab8967452301
my_htole64(0x123456789abcdef) = 0x123456789abcdef
my_be64toh(0x123456789abcdef) = 0xefcdab8967452301
my_le64toh(0x123456789abcdef) = 0x123456789abcdef
Output on a big endian system (expected, but not tested by me):
my_htobe16(0x1234) = 0x1234
my_htole16(0x1234) = 0x3412
my_be16toh(0x1234) = 0x1234
my_le16toh(0x1234) = 0x3412
my_htobe32(0x12345678) = 0x12345678
my_htole32(0x12345678) = 0x78563412
my_be32toh(0x12345678) = 0x12345678
my_le32toh(0x12345678) = 0x78563412
my_htobe64(0x123456789abcdef) = 0x123456789abcdef
my_htole64(0x123456789abcdef) = 0xefcdab8967452301
my_be64toh(0x123456789abcdef) = 0x123456789abcdef
my_le64toh(0x123456789abcdef) = 0xefcdab8967452301
You could use memcpy but that would make your code dependent on the underlying endianess of the CPU. I don't think that's what you want.
My take is that you want to convert to the network endianess of whatever data communications protocol this is, regardless of CPU endianess.
The only way to achieve that is bit shifts. The byte order depends on the target network endianess:
void u32_to_big_endian (uint8_t* dst, const uint32_t src)
{
dst[0] = (uint8_t) ((src >> 24) & 0xFFu);
dst[1] = (uint8_t) ((src >> 16) & 0xFFu);
dst[2] = (uint8_t) ((src >> 8) & 0xFFu);
dst[3] = (uint8_t) ((src >> 0) & 0xFFu);
}
void u32_to_little_endian (uint8_t* dst, const uint32_t src)
{
dst[3] = (uint8_t) ((src >> 24) & 0xFFu);
dst[2] = (uint8_t) ((src >> 16) & 0xFFu);
dst[1] = (uint8_t) ((src >> 8) & 0xFFu);
dst[0] = (uint8_t) ((src >> 0) & 0xFFu);
}
In these functions, it doesn't matter what the CPU endianess is. You can forget about memcpy() and non-standard endianess conversion functions. Full example:
#include <stdio.h>
#include <stdint.h>
void u32_to_big_endian (uint8_t* dst, const uint32_t src)
{
dst[0] = (uint8_t) ((src >> 24) & 0xFFu);
dst[1] = (uint8_t) ((src >> 16) & 0xFFu);
dst[2] = (uint8_t) ((src >> 8) & 0xFFu);
dst[3] = (uint8_t) ((src >> 0) & 0xFFu);
}
void u32_to_little_endian (uint8_t* dst, const uint32_t src)
{
dst[3] = (uint8_t) ((src >> 24) & 0xFFu);
dst[2] = (uint8_t) ((src >> 16) & 0xFFu);
dst[1] = (uint8_t) ((src >> 8) & 0xFFu);
dst[0] = (uint8_t) ((src >> 0) & 0xFFu);
}
int main(void)
{
uint32_t i = 0x00320036u;
uint8_t header[54] = {0};
u32_to_little_endian(&header[2], i);
for(size_t i=0; i<6; i++)
{
printf("%.2x ", (unsigned int)header[i]);
}
}
Output, including the first 2 bytes as zeroes:
00 00 36 00 32 00
Related
Is there any way that i can perform bit manipulation with pointers
Write a function that swaps the highest bits in each nibble of the byte pointed to by the pointer b. (i.e. 0bAxxxBxxx -> 0bBxxxAxxx). I have a predefined function with this prototype: void swapBits(uint8_t* b); The solution that I came up with is not working - void swapBits(uint8_t *b) { uint8_t bit1; uint8_t bit2; uint8_t x; bit1 = (*b >> 4) & 1; bit2 = (*b >> 8) & 1; x = bit1 ^ bit2; x = x << 4 | x << 8; *b = *b ^ x; }
there are couple problems: to get 4th bit from the right you need to shift 3 times (not four) xor is probably not what you need to use here is fixed version: void swapBits(uint8_t* b) { uint8_t bit1; uint8_t bit2; bit1 = ((*b >> 3) & 1) << 7; // get bit from one position and put it into another bit2 = ((*b >> 7) & 1) << 3; *b = (*b & 0x77) | bit1 | bit2; // clear already extracted bits and reset with new values }
You are extracting the low bit in the high nibble and shift out the whole uint8_t for bit2. You need to extract the high bit in both. Example: void swapBits(uint8_t* b) { // shift down the high nibble and get its MSb uint8_t bit1 = (*b >> 4) & 0b1000; // get the MSb in the low nibble and shift it up uint8_t bit2 = (*b & 0b1000) << 4; // remove whatever values the MSbs had and replace them with the new values *b = (*b & 0b01110111) | bit2 | bit1; } 0b for binary literals is a gcc extension (but will become standard in C23). If you can't use it, use a plain 8 instead of 0b1000 and 0x77 instead of 0b01110111. I'm using the extension because it makes it easier to see the patterns. A more generic version where you can supply the mask for the the bits to swap between the nibbles could look like this: uint8_t swapBits(uint8_t b, uint8_t m) { return (b & ~(m << 4 | m)) // remove the bits in the mask | ((b >> 4) & m) // high nibble bits -> low nibble | ((b & m) << 4); // low nibble bits -> high nibble } Demo
Your code does not work because you shift the byte values by 4 and 8 instead of 3 and 7. Here is a modified version of your code: void swapBits(uint8_t *b) { uint8_t bit1 = (*b >> 3) & 1; uint8_t bit2 = (*b >> 7) & 1; uint8_t x = bit1 ^ bit2; *b ^= (x << 3) | (x << 7); } Here is a simplified version: void swapBits(uint8_t *b) { uint8_t x = (*b ^ (*b >> 4)) & 0x08; *b ^= x * 0x11; } Here is an alternative approach that can be used for more multiple bits: void swapBits(uint8_t *b) { uint8_t bit7 = (*b & 0x08) << 4; uint8_t bit3 = (*b & 0x80) >> 4; *b = (*b & 0x77) | bit7 | bit3; }
How to add each byte of an 8-byte long integer?
I'm learning how to use the Intel MMX and SSE instructions in a video application. I have an 8-byte word and I would like to add all 8 bytes and produce a single integer as result. The straightforward method is a series of 7 shifts and adds, but that is slow. What is the fastest way of doing this? Is there an MMX or SSE instruction for this? This is the slow way of doing it unsigned long PackedWord = whatever.... int byte1 = 0xff & (PackedWord); int byte2 = 0xff & (PackedWord >> 8); int byte3 = 0xff & (PackedWord >> 16); int byte4 = 0xff & (PackedWord >> 24); int byte5 = 0xff & (PackedWord >> 32); int byte6 = 0xff & (PackedWord >> 40); int byte7 = 0xff & (PackedWord >> 48); int byte8 = 0xff & (PackedWord >> 56); int sum = byte1 + byte2 + byte3 + byte4 + byte5 + byte6 + byte7 + byte8;
Based on the suggestion of #harold, you'd want something like: #include <emmintrin.h> inline int bytesum(uint64_t pw) { __m64 result = _mm_sad_pu8(*((__m64*) &pw), (__m64) 0LLU); // aka psadbw return _mm_cvtsi64_si32(result); }
You can do this with a horizontal sum-by-multiply after one pairwise reduction: uint16_t bytesum(uint64_t x) { uint64_t pair_bits = 0x0001000100010001LLU; uint64_t mask = pair_bits * 0xFF; uint64_t pair_sum = (x & mask) + ((x >> 8) & mask); return (pair_sum * pair_bits) >> (64 - 16); } This produces much leaner code than doing three pairwise reductions.
I'm not an assembly guru but this code should be a little bit faster on platforms that don't have fancy SIMD instructions: #include <stdint.h> int bytesum(uint64_t pw) { uint64_t a, b, mask; mask = 0x00ff00ff00ff00ffLLU; a = (pw >> 8) & mask; b = pw & mask; pw = a + b; mask = 0x0000ffff0000ffffLLU; a = (pw >> 16) & mask; b = pw & mask; pw = a + b; return (pw >> 32) + (pw & 0xffffffffLLU); } The idea is that you first add every other byte, then every other word, and finally every other doubleworld.
How to convert 64 bit int to binary presentation?
How to convert 64 bit int to binary presentation (big endian)? For reverse task I use these functions: int readInt (struct str *buf) { buf -> cur_len = buf -> cur_len + 4; return (((buf -> data[buf -> cur_len - 3 ] & 0xff) << 24) | ((buf -> data[buf -> cur_len - 2 ] & 0xff) << 16) | ((buf -> data[buf -> cur_len - 1 ] & 0xff) << 8) | ((buf -> data[buf -> cur_len ] & 0xff) << 0)); }; long unsigned int 32Bit(struct str *buf) { // 32 return ((long unsigned int)readInt(buf)) & 0xffffffffL; }; long unsigned int 64Bit(struct str *buffer) { //64 long unsigned int result = 32Bit(buf); result *= 4294967296.0; return result; }
Serialising a 64 bit unsigned number into an array of unsigned char, storing 8 bits in each in big-endian order, can be done like so: void serialise_64bit(unsigned char dest[8], unsigned long long n) { dest[0] = (n >> 56) & 0xff; dest[1] = (n >> 48) & 0xff; dest[2] = (n >> 40) & 0xff; dest[3] = (n >> 32) & 0xff; dest[4] = (n >> 24) & 0xff; dest[5] = (n >> 16) & 0xff; dest[6] = (n >> 8) & 0xff; dest[7] = (n >> 0) & 0xff; }
You shouldn't use built-in types for serialization; instead, when you need to know the exact size of a type, you need fixed-width types: #include <stdint.h> unsigned char buf[8]; // 64-bit raw data uint64_t little_endian_value = (uint64_t)buf[0] + ((uint64_t)buf[1] << 8) + ((uint64_t)buf[2] << 16) + ... + ((uint64_t)buf[7] << 56); uint64_t big_endian_value = (uint64_t)buf[7] + ((uint64_t)buf[6] << 8) + ((uint64_t)buf[5] << 16) + ... + ((uint64_t)buf[0] << 56); Similarly for 32-bit values, use uint32_t there. Make sure your source buffer uses unsigned chars.
reverse the bits using bit field in c language?
how to reverse the bits using bit wise operators in c language Eg: i/p: 10010101 o/p: 10101001
If it's just 8 bits: u_char in = 0x95; u_char out = 0; for (int i = 0; i < 8; ++i) { out <<= 1; out |= (in & 0x01); in >>= 1; } Or for bonus points: u_char in = 0x95; u_char out = in; out = (out & 0xaa) >> 1 | (out & 0x55) << 1; out = (out & 0xcc) >> 2 | (out & 0x33) << 2; out = (out & 0xf0) >> 4 | (out & 0x0f) << 4; figuring out how the last one works is an exercise for the reader ;-)
Knuth has a section on Bit reversal in The Art of Computer Programming Vol 4A, bitwise tricks and techniques. To reverse the bits of a 32 bit number in a divide and conquer fashion he uses magic constants u0= 1010101010101010, (from -1/(2+1) u1= 0011001100110011, (from -1/(4+1) u2= 0000111100001111, (from -1/(16+1) u3= 0000000011111111, (from -1/(256+1) Method credited to Henry Warren Jr., Hackers delight. unsigned int u0 = 0x55555555; x = (((x >> 1) & u0) | ((x & u0) << 1)); unsigned int u1 = 0x33333333; x = (((x >> 2) & u1) | ((x & u1) << 2)); unsigned int u2 = 0x0f0f0f0f; x = (((x >> 4) & u2) | ((x & u2) << 4)); unsigned int u3 = 0x00ff00ff; x = (((x >> 8) & u3) | ((x & u3) << 8)); x = ((x >> 16) | (x << 16) mod 0x100000000); // reversed The 16 and 8 bit cases are left as an exercise to the reader.
Well, this might not be the most elegant solution but it is a solution: int reverseBits(int x) { int res = 0; int len = sizeof(x) * 8; // no of bits to reverse int i, shift, mask; for(i = 0; i < len; i++) { mask = 1 << i; //which bit we are at shift = len - 2*i - 1; mask &= x; mask = (shift > 0) ? mask << shift : mask >> -shift; res |= mask; // mask the bit we work at at shift it to the left } return res; } Tested it on a sheet of paper and it seemed to work :D Edit: Yeah, this is indeed very complicated. I dunno why, but I wanted to find a solution without touching the input, so this came to my haead
Integer to IP Address - C
I'm preparing for a quiz, and I have a strong suspicion I may be tasked with implementing such a function. Basically, given an IP address in network notation, how can we get that from a 32 bit integer into a string in it's dotted decimal notation (something like 155.247.182.83)...? Obviously we can't be using any type of inet functions either...I'm stumped!
You actually can use an inet function. Observe. main.c: #include <arpa/inet.h> main() { uint32_t ip = 2110443574; struct in_addr ip_addr; ip_addr.s_addr = ip; printf("The IP address is %s\n", inet_ntoa(ip_addr)); } The results of gcc main.c -ansi; ./a.out is The IP address is 54.208.202.125 Note that a commenter said this does not work on Windows.
Here's a simple method to do it: The (ip >> 8), (ip >> 16) and (ip >> 24) moves the 2nd, 3rd and 4th bytes into the lower order byte, while the & 0xFF isolates the least significant byte at each step. void print_ip(unsigned int ip) { unsigned char bytes[4]; bytes[0] = ip & 0xFF; bytes[1] = (ip >> 8) & 0xFF; bytes[2] = (ip >> 16) & 0xFF; bytes[3] = (ip >> 24) & 0xFF; printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]); } There is an implied bytes[0] = (ip >> 0) & 0xFF; at the first step. Use snprintf() to print it to a string.
Another approach: union IP { unsigned int ip; struct { unsigned char d; unsigned char c; unsigned char b; unsigned char a; } ip2; }; ... char ips[20]; IP ip; ip.ip = 0xAABBCCDD; sprintf(ips, "%x.%x.%x.%x", ip.ip2.a, ip.ip2.b, ip.ip2.c, ip.ip2.d); printf("%s\n", ips);
Hint: break up the 32-bit integer to 4 8-bit integers, and print them out. Something along the lines of this (not compiled, YMMV): int i = 0xDEADBEEF; // some 32-bit integer printf("%i.%i.%i.%i", (i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF);
This is what I would do if passed a string buffer to fill and I knew the buffer was big enough (ie at least 16 characters long): sprintf(buffer, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip ) & 0xFF); This would be slightly faster than creating a byte array first, and I think it is more readable. I would normally use snprintf, but IP addresses can't be more than 16 characters long including the terminating null. Alternatively if I was asked for a function returning a char*: char* IPAddressToString(int ip) { char[] result = new char[16]; sprintf(result, "%d.%d.%d.%d", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip ) & 0xFF); return result; }
#include "stdio.h" void print_ip(int ip) { unsigned char bytes[4]; int i; for(i=0; i<4; i++) { bytes[i] = (ip >> i*8) & 0xFF; } printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]); } int main() { int ip = 0xDEADBEEF; print_ip(ip); }
From string to int and back const char * s_ip = "192.168.0.5"; unsigned int ip; unsigned char * c_ip = (unsigned char *)&ip; sscanf(s_ip, "%hhu.%hhu.%hhu.%hhu", &c_ip[3], &c_ip[2], &c_ip[1], &c_ip[0]); printf("%u.%u.%u.%u", ((ip & 0xff000000) >> 24), ((ip & 0x00ff0000) >> 16), ((ip & 0x0000ff00) >> 8), (ip & 0x000000ff)); %hhu instructs sscanf to read into unsigned char pointer; (Reading small int with scanf) inet_ntoa from glibc char * inet_ntoa (struct in_addr in) { unsigned char *bytes = (unsigned char *) ∈ __snprintf (buffer, sizeof (buffer), "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); return buffer; }
My alternative solution with subtraction :) void convert( unsigned int addr ) { unsigned int num[OCTET], next_addr[OCTET]; int bits = 8; unsigned int shift_bits; int i; next_addr[0] = addr; shift_bits -= bits; num[0] = next_addr[0] >> shift_bits; for ( i = 0; i < OCTET-1; i ++ ) { next_addr[i + 1] = next_addr[i] - ( num[i] << shift_bits ); // next subaddr shift_bits -= bits; // next shift num[i + 1] = next_addr[i + 1] >> shift_bits; // octet } printf( "%d.%d.%d.%d\n", num[0], num[1], num[2], num[3] ); }
void ul2chardec(char*pcIP, unsigned long ulIPN){ int i; int k=0; char c0, c1; for (i = 0; i<4; i++){ c0 = ((((ulIPN & (0xff << ((3 - i) * 8))) >> ((3 - i) * 8))) / 100) + 0x30; if (c0 != '0'){ *(pcIP + k) = c0; k++; } c1 = (((((ulIPN & (0xff << ((3 - i) * 8))) >> ((3 - i) * 8))) % 100) / 10) + 0x30; if (!(c1 =='0' && c0=='0')){ *(pcIP + k) = c1; k++; } *(pcIP +k) = (((((ulIPN & (0xff << ((3 - i) * 8)))) >> ((3 - i) * 8))) % 10) + 0x30; k++; if (i<3){ *(pcIP + k) = '.'; k++;} } *(pcIP + k) = 0; // pcIP should be x10 bytes }