You are invited to Log in or Register a free Frihost Account!

# Merging Integer Variables

imagefree
I am confused how to explain the problem, here is a try. If you do not get what i wanted to explain, please ask me.

I have data in 4 variables. Each is 4 byte integer. I want to Merge the variables and return a single 16 byte number.

For example each variables has 4 bytes of data. I want all bytes to be concatenated so that the final output it gives is a 16 byte long integer. I AM NOT Asking for string concatenation.

I am not looking for something like this: (because it will return a string that may have a size of longer than 16 bytes)

 Code: \$int1 = 123456789; \$int2 = 567890123; \$int3 = 901234567; \$int4 = 345678901; echo \$int1.\$int2.\$int3.\$int4; //OUTPUT: 123456789567890123901234567345678901

What i want is that 16 bytes are arranged in a series, that can be seen as a 16 byte long integer.

 Code: \$int1 = 123456789; \$int2 = 567890123; \$int3 = 901234567; \$int4 = 345678901; echo some_convertor( \$int1 , \$int2 , \$int3 , \$int4 ); //OUTPUT: [byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte][byte] //cant figure out what the output will be

And i hope we can see this 16 byte long data as number with php.

The solution need to be compatible with php since php supports 4 byte integers, and 16 bytes integer need to be in string data type (or something else you might know better).

Edit: I have found a solution: for C#
but the problem still stands that you cannot add integers so long (for 16 bytes) with reliability.
Any solution for this? Also that I don't want to use Big Int at the moment.
sonam
I don't understand. You are wrote:

 Quote: I have data in 4 variables. Each is 4 byte integer. I want to Merge the variables and return a single 16 byte number.

But in your example integers are with nine bites not four?!
If you have hexadecimal 4 byte variables then we need this information, too.

Sonam
jmraker
You could use the arbitary math functions that let you work with huge numbers
http://us.php.net/manual/en/book.bc.php

I've never used it before in PHP but I'm sure its just like the bc command in linux

I'm guessing the value of \$a0 is what you're expecting.

For me the value of \$a0 is a float. It might be an integer on the 64-bit version of php

I don't have the bcMath library enabled so I haven't run it or seen that it works.

 Code:

PHP has 0x123456789abcdef0 as float( 1.3117684674638E+18 )

I used bc to figure out the integer value
(4660 * 281474976710656) + (22136 * 4294967296) + (39612 * 65536) + 57072 =
1,311,768,467,463,790,320
imagefree
sonam wrote:
I don't understand. You are wrote:

 Quote: I have data in 4 variables. Each is 4 byte integer. I want to Merge the variables and return a single 16 byte number.

But in your example integers are with nine bites not four?!
If you have hexadecimal 4 byte variables then we need this information, too.

Sonam

Its 9 digits, not 9 bytes. 4 bytes can store unsigned number of about 4 Billion. See PHP_INT_MAX.
imagefree
thanks jmraker.

Edit: Although its what I wanted, this is not a standard solution because bc not always included in php. I was looking for some work around to get this done.
jmraker
Unless it runs in 64-bit php, the solution would have to include math functions that replace the built in math because 32-bit cpus can't store 64-bit integers. The math routines would have to do the math by hand as you were taught in school on paper, but with huge numbers. I once tried to make a program like bc a long time ago before I got onto the internet and I never got the all the bugs worked out. I remember it being easy until I started having it do negative numbers and division being the one that worked in all my tests and the others failed with decimal places or + - sign, things you'd probably wouldn't need to worry about.

I think it's either go 64-bit, use bcMath, or write the functions that mimic bcMath.