Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Converting between vectors

Reply
Thread Tools

Converting between vectors

 
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
LR <(E-Mail Removed)> writes:

>
> Can you please be more specific?


The bugs are exactly what you point out later, I never noticed because
the sizes where always such that it always worked anyway...

> It looks to me like you want to pack or unpack the values in input to
> res. If that's the case the above line will not work in all cases.
> Consider if size_inp == 2 and input.size() == 3 and size_out == 4.


Yes I want to do that, any suggestions how to do it more generally and
in a more correct way?
 
Reply With Quote
 
 
 
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
LR <(E-Mail Removed)> writes:

> I neglected to mention that you might want to check to see if INP and
> OUT are integer types using std::numeric_limits::is_integer.
>


Why should I check? In general I should not care to check...
Actually the whole conversion thing only happens from

type T -> unsigned char
and viceversa, no I can always suppose that the input or the output has
size 1 (not sure it makes a difference).
 
Reply With Quote
 
 
 
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
Noah Roberts <(E-Mail Removed)> writes:

> On 4/21/2011 8:36 AM, Andrea Crotti wrote:
>> Boost unfortunately is not the answer since I can't use it in the
>> project.

>
> Still.... Use boost.


I think I should add a signature "I can't use Boost".
Is not that I don't want to, but the strict requirement is that I can
only use the standard library, and actually considered where it will
deployed it makes perfectly sense.

Boost is great, but suggesting it when someone already said that is not
an option doesn't help much unfortunately...
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
"Alf P. Steinbach /Usenet" <(E-Mail Removed)> writes:

>
> Could you at least hint about about the kind of environment where it
> will be employed where it makes perfect sense to not use Boost?
>


Network routers in an experimental testbed, asking new software there
is not easy and surely takes too long for the time I have left.
And it was a decision taken in the beginning, so discuss about it is
just a waste of time...

>
> huh.
>


Well I think I wrote 200 times "I can't use boost", that's why
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
Ian Collins <(E-Mail Removed)> writes:

> On 04/22/11 04:32 AM, Andrea Crotti wrote:
>> A simpler question while I try to figure out how to do this:
>> How do I make 2 ints fit in one long and then extract them again?

>
> You can't portably. You would have to use fixed width types.
>
> I'd start with a compile time "assert" like
>
> const bool checkSizes = 1/(sizeof(long)==2*sizeof(int));
>
> followed by
>
> const size_t intBits = sizeof(int)*CHAR_BIT;


This is just a toy example but I wanted to understand how to do it
correctly...

Like this it seems to work fine, is that correct in theory?
Are there other ways to achieve the same thing in general?

--8<---------------cut here---------------start------------->8---
long intsToLong(int x, int y)
{
long tmp = x;
tmp <<= int_bits;
tmp += y;
return tmp;
}

void longToInts(long l)
{
int second = l >> int_bits;
long mask = (2 >> (int_bits + 1)) - 1;
int first = l & mask;
std::cout << " first " << first << " second " << second << std::endl;
}
--8<---------------cut here---------------end--------------->8---
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-22-2011
* Andrea Crotti, on 22.04.2011 11:27:
> Noah Roberts<(E-Mail Removed)> writes:
>
>> On 4/21/2011 8:36 AM, Andrea Crotti wrote:
>>> Boost unfortunately is not the answer since I can't use it in the
>>> project.

>>
>> Still.... Use boost.

>
> I think I should add a signature "I can't use Boost".
> Is not that I don't want to, but the strict requirement is that I can
> only use the standard library, and actually considered where it will
> deployed it makes perfectly sense.


Could you at least hint about about the kind of environment where it will be
employed where it makes perfect sense to not use Boost?


> Boost is great, but suggesting it when someone already said that is not
> an option doesn't help much unfortunately...


huh.


Cheers

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-22-2011
On 04/22/11 09:49 PM, Andrea Crotti wrote:
> Ian Collins<(E-Mail Removed)> writes:
>
>> On 04/22/11 04:32 AM, Andrea Crotti wrote:
>>> A simpler question while I try to figure out how to do this:
>>> How do I make 2 ints fit in one long and then extract them again?

>>
>> You can't portably. You would have to use fixed width types.
>>
>> I'd start with a compile time "assert" like
>>
>> const bool checkSizes = 1/(sizeof(long)==2*sizeof(int));
>>
>> followed by
>>
>> const size_t intBits = sizeof(int)*CHAR_BIT;

>
> This is just a toy example but I wanted to understand how to do it
> correctly...
>
> Like this it seems to work fine, is that correct in theory?
> Are there other ways to achieve the same thing in general?


Mask and shift is the idiomatic, portable, solution.

> --8<---------------cut here---------------start------------->8---
> long intsToLong(int x, int y)
> {
> long tmp = x;
> tmp<<= int_bits;
> tmp += y;
> return tmp;
> }
>
> void longToInts(long l)
> {
> int second = l>> int_bits;
> long mask = (2>> (int_bits + 1)) - 1;


Have you by any chance looked at the value of mask? Hint - what is 2
left shifted 31 places?

--
Ian Collins
 
Reply With Quote
 
Andrea Crotti
Guest
Posts: n/a
 
      04-22-2011
Ian Collins <(E-Mail Removed)> writes:

>
>> --8<---------------cut here---------------start------------->8---
>> long intsToLong(int x, int y)
>> {
>> long tmp = x;
>> tmp<<= int_bits;
>> tmp += y;
>> return tmp;
>> }
>>
>> void longToInts(long l)
>> {
>> int second = l>> int_bits;
>> long mask = (2>> (int_bits + 1)) - 1;

>
> Have you by any chance looked at the value of mask? Hint - what is 2
> left shifted 31 places?


Yes sure I was shifting in the wrong direction, thanks

So and what if the size of the types is not multiple of each other?
For example type X is 6 bytes and type Y is 4 bytes.

Then what is the best way to pack data from one type to the other?
 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      04-22-2011
Andrea Crotti wrote:
> LR <(E-Mail Removed)> writes:
>
>>
>> Can you please be more specific?

>
> The bugs are exactly what you point out later, I never noticed because
> the sizes where always such that it always worked anyway...
>
>> It looks to me like you want to pack or unpack the values in input to
>> res. If that's the case the above line will not work in all cases.
>> Consider if size_inp == 2 and input.size() == 3 and size_out == 4.

>
> Yes I want to do that, any suggestions how to do it more generally and
> in a more correct way?


I think you'll have to fix the math problem and also you'll want to
maintain the number of characters you're converting.

Personally, I don't think memcpy is a bad way to do this, but perhaps
there is a better way.

LR

 
Reply With Quote
 
LR
Guest
Posts: n/a
 
      04-22-2011
Andrea Crotti wrote:
> LR <(E-Mail Removed)> writes:
>
>> I neglected to mention that you might want to check to see if INP and
>> OUT are integer types using std::numeric_limits::is_integer.
>>

>
> Why should I check? In general I should not care to check...
> Actually the whole conversion thing only happens from
>
> type T -> unsigned char
> and viceversa, no I can always suppose that the input or the output has
> size 1 (not sure it makes a difference).


This makes me feel a little uncertain as to the problem you actually
want to solve, since what you posted converts packs and unpacks long and
int.

LR
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ primer statement about vectors containing vectors pauldepstein@att.net C++ 3 03-26-2008 06:22 PM
Check difference between two vectors shapper ASP .Net 1 02-15-2008 04:38 PM
Ruby, SWIG and C++: how to properly wrap vector of vectors of doubles (2D vectors)? Ruby 0 09-14-2005 05:47 PM
Converting between two different XML forms cbongior@stny.rr.com Java 3 08-05-2005 08:16 PM
Math problem - converting between arbitrary bases in perl - help! ! aaa Perl 1 05-28-2004 01:42 AM



Advertisments