Convert char[] to int[]

K

Koolrans

Hi All,

Don't know if this is covered somewhere else. Could not find it. I
have a problem for which I need a solution.

I have an array of char chr[16]. I want to convert this to an array
of uint32 it[4]. What I require is that for the first integer, the
value would be of the four chars. This means, the first 8 bits will be
tmp[0], the second 8 bits would be tmp[1] and so on.

Thanks for all your help.

Koolrans
 
D

Default User

Koolrans said:
Hi All,

Don't know if this is covered somewhere else. Could not find it. I
have a problem for which I need a solution.

I have an array of char chr[16]. I want to convert this to an array
of uint32 it[4]. What I require is that for the first integer, the
value would be of the four chars. This means, the first 8 bits will be
tmp[0], the second 8 bits would be tmp[1] and so on.

If data in the char array is already in the correct byte order, and
properly aligned, and uint32 is some 32-bit integer datatype on your
system (it's not standard), then all you have do is a memcpy().

However, if things are not that way, you'll have to do some work.
Without details (like where how this char array was created and filled)
it's difficult to give any more information.




Brian
 
J

John Carson

Koolrans said:
Hi All,

Don't know if this is covered somewhere else. Could not find it. I
have a problem for which I need a solution.

I have an array of char chr[16]. I want to convert this to an array
of uint32 it[4]. What I require is that for the first integer, the
value would be of the four chars. This means, the first 8 bits will be
tmp[0], the second 8 bits would be tmp[1] and so on.

Not sure what you mean by the "first" 8 bits: the most significant or the
least significant? If the most significant, then the following should work.
If the least significant, reverse the order of the indices in the convert
function.

int convert(const char *array)
{
return array[0]<<24 | array[1]<<16 | array[2]<<8 | array[3];
}

int main()
{
for(int i=0; i<4; ++i)
it = convert(chr+i*4);
return 0;
}
 
K

Koolrans

Koolrans said:
Don't know if this is covered somewhere else. Could not find it. I
have a problem for which I need a solution.
I have an array of char chr[16]. I want to convert this to an array
of uint32 it[4]. What I require is that for the first integer, the
value would be of the four chars. This means, the first 8 bits will be
tmp[0], the second 8 bits would be tmp[1] and so on.

If data in the char array is already in the correct byte order, and
properly aligned, and uint32 is some 32-bit integer datatype on your
system (it's not standard), then all you have do is a memcpy().

However, if things are not that way, you'll have to do some work.
Without details (like where how this char array was created and filled)
it's difficult to give any more information.

Brian

Currently, I am storing an ipv6 address in tmp[16]. I want to compare
4 bytes at a time and so want to convert it to it[4]. The code do
needs to run on Windows, Linux and Solaris.

Thanks,
 
M

Martijn van Buul

* Koolrans:
Currently, I am storing an ipv6 address in tmp[16]. I want to compare
4 bytes at a time and so want to convert it to it[4]. The code do
needs to run on Windows, Linux and Solaris.

"windows, Linux and Solaris" is of no importance. What matters is the
endianness of your target, be it little endian (x86...) or bigendian (ppc,
Sparc..). At least two of your target OSes are available in both (Solaris
and Linux, not sure about Windows)

If all you want to do is compare it to a second address for equality, then
the byte order doesn't matter, and you could suffice with a simple cast.

The question is: "Why??". You'll run into all kinds of issues (How big is
an int? Why not using two 64-bit integers on those platforms supporting that?)
while there is very little gain. Use memcmp.

If you really insist of going the uint32_t way, you should consider using
the ntohl()/htonl() combo, as it is the correct cross-platform approach as
it will take care of endianness for you.

(which is OT, but I think it's available on all platforms).
 
P

persenaama

Currently, I am storing an ipv6 address in tmp[16]. I want to compare
4 bytes at a time and so want to convert it to it[4]. The code do
needs to run on Windows, Linux and Solaris.

std::memcmp() ? Or you need predicate for something else..?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top