Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Converting an array of uint8 values to uint16's

Subject: Converting an array of uint8 values to uint16's

From: jrenfree

Date: 15 Aug, 2011 21:02:12

Message: 1 of 7

I'll try to explain this in a clear way. Let's say that I have an
array of uint8 values, e.g.

A = [4; 8; 16; 32];

What I'd like to do is combine successive values and treat them as
uint16's, such that our new array, B, would be half the length. If we
think about it in terms of binary terms, it would look like the
following:

dec2bin(A, 8)

ans =

00000100
00001000
00010000
00100000

B = ['0000010000001000'; '0001000000100000'];

bin2dec(B)

ans =

        1032
        4128

I'm currently implementing this by using the following code:

temp = dec2bin(A, 8);
temp = strcat(temp(1:2:end,:), temp(2:2:end,:));
B = bin2dec(temp);

When my initial array A is quite large, this method seems to slow down
by program by quite a bit. I'm wondering if there is a quicker way to
combine these uint8 values to obtain uint16 values without needing to
convert to and from binary. I'm thinking it would involve the use of
typecast, cast, uint8, and uint16 functions, but I can't figure it
out.

Thanks!

Subject: Converting an array of uint8 values to uint16's

From: Jan Simon

Date: 15 Aug, 2011 21:21:13

Message: 2 of 7

Dear jrenfree,
 
  A = [4, 8, 16, 32];
  B = typecast(uint8(A), 'uint16')

Kind regards, Jan

Subject: Converting an array of uint8 values to uint16's

From: Bruno Luong

Date: 15 Aug, 2011 21:22:13

Message: 3 of 7

>> A = [4; 8; 16; 32];
>> typecast(reshape(flipud(reshape(uint8(A), 2, [])), 1, []), 'uint16')

ans =

   1032 4128

% Bruno

Subject: Converting an array of uint8 values to uint16's

From: dpb

Date: 15 Aug, 2011 21:28:16

Message: 4 of 7

On 8/15/2011 4:02 PM, jrenfree wrote:
> I'll try to explain this in a clear way. Let's say that I have an
> array of uint8 values, e.g.
>
> A = [4; 8; 16; 32];
>
> What I'd like to do is combine successive values and treat them as
> uint16's, such that our new array, B, would be half the length. If we
> think about it in terms of binary terms, it would look like the
> following:
>
> dec2bin(A, 8)
>
> ans =
>
> 00000100
> 00001000
> 00010000
> 00100000
>
> B = ['0000010000001000'; '0001000000100000'];
>
> bin2dec(B)
>
> ans =
>
> 1032
> 4128
...

Brute force...

for idx=1:2:length(A)
   B(idx)=256*A(idx)+A(idx+1);
end

I'm sure newer release w/ bsxfun or similar would be one-liner, but my
version predates same...

--

Subject: Converting an array of uint8 values to uint16's

From: jrenfree

Date: 15 Aug, 2011 21:46:50

Message: 5 of 7

On Aug 15, 2:22 pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
wrote:
> >> A = [4; 8; 16; 32];
> >> typecast(reshape(flipud(reshape(uint8(A), 2, [])), 1, []), 'uint16')
>
> ans =
>
>    1032   4128
>
> % Bruno

This works great. I originally had what Jan suggested but was getting
the wrong answer, so I figured I just didn't know what I was doing. I
still don't quite understand why you need to reorder the uint8 values
like you do. Is that because of the Endianness?

Thanks everyone!

Subject: Converting an array of uint8 values to uint16's

From: Bruno Luong

Date: 15 Aug, 2011 22:02:29

Message: 6 of 7

jrenfree <jrenfree@gmail.com> wrote in message

> Is that because of the Endianness?
>

Yes. The uint16 has the dominant byte in the second position, put the weak byte in exposure.

Bruno

Subject: Converting an array of uint8 values to uint16's

From: James Tursa

Date: 15 Aug, 2011 22:47:11

Message: 7 of 7

jrenfree <jrenfree@gmail.com> wrote in message <de8df235-73f3-4733-8ee6-1feb595f2d06@o9g2000vbo.googlegroups.com>...
> On Aug 15, 2:22 pm, "Bruno Luong" <b.lu...@fogale.findmycountry>
> wrote:
> > >> A = [4; 8; 16; 32];
> > >> typecast(reshape(flipud(reshape(uint8(A), 2, [])), 1, []), 'uint16')
> >
> > ans =
> >
> >    1032   4128
> >
> > % Bruno
>
> This works great. I originally had what Jan suggested but was getting
> the wrong answer, so I figured I just didn't know what I was doing. I
> still don't quite understand why you need to reorder the uint8 values
> like you do. Is that because of the Endianness?
>
> Thanks everyone!

FYI, if you need to reorder because of endian differences typically one would use typecast followed by swapbytes.

James Tursa

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us