# Thread: Problem in Converting Unsigned long to String

1. ## Problem in Converting Unsigned long to String

Hi All,
I am facing the Problem while converting from unsigned long to string. can any body help me out?

The thing is i have an unsigned long. I have to convert that to string.

Consider,
unsigned long type=0x11;

The unsigned long will take 4 bytes. I want to convert this to string of 4 Bytes.

Again if I convert the string of 4 Bytes to unsigned long I have to get the origianal value.

i tried using sprintf(str, "%lu", sizeof(unsigned long));

When I tried to get the first four bytes and convert it back to unsigned long it is not giving the original value.

Can anybody help me out.. Please?

2. Assuming your value is 9999 or less, the approach is
Code:
```char buffer[5];
sprintf(buffer, "%lu", value);```
This will produce a string of maximum length 4 (plus an additional zero byte).

If the value is 10000 or more (i.e. 5 or more digits) it won't squeeze into a string of length 4 in a decimal format.

3. Thanks Grumpy...

But My Problem is that if i give

unsigned long type = 1;

I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like

0001

And if
unsigned long type=12345;

I have to get this also in 4 Bytes string.

The unsigned long stores the 12345 in 4 bytes. So if i convert that also in to a string i have to get in 4 bytes.

Can any body help me out..

4. Code:
`sprintf(str, "%lu", sizeof(unsigned long));`
here you will get 4 in str.
unsigned long type = 1;
I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like
0001
And if
unsigned long type=12345;
I have to get this also in 4 Bytes string.
In that case, you should be using
Code:
`sprintf(str, "%lu", type );`
Code:
`sprintf(str, "%lu", sizeof(unsigned long));`

5. Originally Posted by cprogrammer_18
unsigned long type = 1;
I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like
0001
And if
unsigned long type=12345;
I have to get this also in 4 Bytes string.
Try this
Code:
```unsigned long type=1;
sprintf(str, "%04lu", type );```
This will output 0001.
But if you want to squeeze a 5 digit number into 4 chars then you are out of luck.
if you want the value 12345 output only 1234 or maybe 2345 then you have to write your own function that handles truncation the way you want it. (but then there is no way to restore the original value from the string )
Maybe you should tell us again what you try to accomplish.
Fact is that not every unsigned int fits into a string with the length of 4. storing it as hex will need a 9 char buffer. decimal 11 ( including the '\0' terminator ).

BTW if what you want is pass an unsigned int to a function like fwrite() let us know.
Kurt

6. Thanks Zuk..

Well.. I will explain in detail...
I have a structure

Code:
```struct payload{
unsigned long type;
unsigned long length;
unsigned char value[1024];
};```
I have to read the structure element and concatenate in to string. I have to pass this string in to a socket. On the other end I have to read it.

So I know the unsigned long occupies 4 Bytes and while I read on the other end I have to read the first 4 bytes and convert back in to unsigned long.. and the next bytes and so on...

So how to do this? Any Ideas...

7. Contd...

I dont want to use any delimiters while concatenating..

8. That's what I thought.
You don't have to convert your struct to a string you just cast it's address to a char *
for sending you do
Code:
```payload p;
send( (char*)&p, sizeof(p));```
at the receiving end you do

Code:
```char  buffer [256];
// you access the fields like this
printf("type = %lu\n", pp->type );```
Hope that is what you want.
Kurt

9. Thanks a lot...

I got the solution...