• C

How to concatenate ints in C?

My program runs rand(), gets it as 16 bit,

eg rand() gives me 12F3
another rand() gives me 564A

so if I do printf("%X%X\n", rand() rand());
I get exactly what I want, 32 bit numbers.

The thing is, I want this in a variable, so I tried to be creative and did:

x = printf("%X%X\n", rand() rand())

but unfortunately didnt work.

So I tried

int x;
int y;
int z;

x = rand()
y = rand()

strcat(x, y);

But C wont accept mixing integer types, now what is my option?

Thanks in advance,

Markov
LVL 1
markov123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Hi markov123,

snprintf (buffer, BUFFER_LENGTH, "%X%X",rand(),rand());

Cheers!
sunnycoder
callrsCommented:
http://ddas77.tripod.com/guide/programming/c/sprintf.html     C fundas! the sprintf( ) function...
callrsCommented:
sunnnycoder:

snprintf --> sprintf

sunnycoderCommented:
Hi callrs,

snprintf is safer version of sprintf where you can prevent buffer overruns by specifying the max length (buffer capacity) of string to be written to buffer.

Cheers!
sunnycoder
callrsCommented:
ok nvm, it seems snprintf is a function too
markov123Author Commented:
Hi Sunnycoder,

Am still not getting the results I want, I must be doing something wrong.

Please check the syntax below.

   int x;
   int y;
   int z[2];
 
   
   x = 0X1111;
   y = 0X3222;
 
   printf("x: %0x, y: %0x\n", x, y);

   sprintf(z, "%0x%0x\n", x, y);
 
   printf("%0x\n", z);

z should display 11113222, but instead i get something entirely different...
sunnycoderCommented:
z should be a char array or char * ... It will be printed using %s and not %x format specifier .. it will be a string ...

s -> string
n -> number of characters
print -> print
f -> formatted
sunnycoderCommented:
  char z[64];
   sprintf(z, "%0x%0x\n", x, y);
 
   printf("%s", z);
Infinity08Commented:
I think what you're looking for is :

    unsigned int x = ((rand() << 16) & 0xFFFF0000) + (rand() & 0x0000FFFF);

Is that what you need ?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
markov123Author Commented:
Infinity,

This is exactly what I wanted, thanks a lot.


sunnycoder,

I want z as an int, so when I did: printf("%0xz\n", (int) z); I was getting different results.

Anyway, its working fine now, I thank you both for your efforts.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.