Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 26th, 2007, 11:05 PM
Koolrans
Guest
 
Posts: n/a
Default Convert char[] to int[]


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

  #2  
Old February 26th, 2007, 11:25 PM
Default User
Guest
 
Posts: n/a
Default Re: Convert char[] to int[]

Koolrans wrote:
Quote:
>
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
  #3  
Old February 26th, 2007, 11:55 PM
John Carson
Guest
 
Posts: n/a
Default Re: Convert char[] to int[]

"Koolrans" <koolrans@gmail.comwrote in message
news:1172530688.401230.32510@p10g2000cwp.googlegro ups.com
Quote:
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[i] = convert(chr+i*4);
return 0;
}


--
John Carson


  #4  
Old February 27th, 2007, 12:25 AM
Koolrans
Guest
 
Posts: n/a
Default Re: Convert char[] to int[]

On Feb 26, 3:12 pm, "Default User" <defaultuse...@yahoo.comwrote:
Quote:
Koolrans wrote:
>
Quote:
Hi All,
>
Quote:
Don't know if this is covered somewhere else. Could not find it. I
have a problem for which I need a solution.
>
Quote:
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,

  #5  
Old February 27th, 2007, 10:25 AM
Martijn van Buul
Guest
 
Posts: n/a
Default Re: Convert char[] to int[]

* Koolrans:
Quote:
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).

--
Martijn van Buul - pino@dohd.org
  #6  
Old February 27th, 2007, 10:45 AM
persenaama
Guest
 
Posts: n/a
Default Re: Convert char[] to int[]

On Feb 27, 2:11 am, "Koolrans" <koolr...@gmail.comwrote:
Quote:
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..?

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles