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:
concatenate?

Subject: concatenate?

From: Deanna

Date: 22 Sep, 2009 16:15:22

Message: 1 of 5

I am reading two columns of data from a spreadsheet. The data from each column is an integer representation of a hex number. In MATLAB, I convert using dec2hex. So now I have two columns of hex numbers. I would like to put those hex numbers together to make a full word out of the two hex half words.

For instance, in one row of the column I have 16944 and 12257. I convert to hex to get 4230 and 2FE1. What I really want is 42302FE1 (to make a full word). I don't know how to do this.

Once I have a full word I would like to convert to a decimal number. Do I use hex2dec for that? Or hex2num?

Please advise. Thank you.

Subject: concatenate?

From: David Young

Date: 22 Sep, 2009 17:23:06

Message: 2 of 5

Your questions isn't entirely clear, because "hex number" and "decimal number" aren't well-defined terms. Hex is a notation for writing down or printing out a number (or representing it as a string in memory) but it isn't usually needed in the course of a computation. What I think you're asking is just how to combine two numbers that represent the 16-bit halves of a 32-bit word.

If you read in the first column of your file to c1, and the second column to c2, then, if I've understood your question correctly, you can combine them just by doing:

       c = c1 * 65536 + c2;

or, perhaps more clearly,

       c = bitor( bitshift(c1, 16), c2 );

and not using hex at all.

Hope that helps.

Subject: concatenate?

From: Yadunandan

Date: 27 Jan, 2012 01:20:11

Message: 3 of 5

"David Young" wrote in message <h9b15q$mgf$1@fred.mathworks.com>...
> Your questions isn't entirely clear, because "hex number" and "decimal number" aren't well-defined terms. Hex is a notation for writing down or printing out a number (or representing it as a string in memory) but it isn't usually needed in the course of a computation. What I think you're asking is just how to combine two numbers that represent the 16-bit halves of a 32-bit word.
>
> If you read in the first column of your file to c1, and the second column to c2, then, if I've understood your question correctly, you can combine them just by doing:
>
> c = c1 * 65536 + c2;
>
> or, perhaps more clearly,
>
> c = bitor( bitshift(c1, 16), c2 );
>
> and not using hex at all.
>
> Hope that helps.

I have the same problem. Thanks alot David. But, what if one of the values is negative number. In this case, could you please suggest me a solution ? It would be really helpful.

Thank You

Subject: concatenate?

From: Wyatt

Date: 6 Nov, 2012 15:40:08

Message: 4 of 5

"Yadunandan " <nandu@mando.com> wrote in message <jfsu4b$4b0$1@newscl01ah.mathworks.com>...
> "David Young" wrote in message <h9b15q$mgf$1@fred.mathworks.com>...
> > Your questions isn't entirely clear, because "hex number" and "decimal number" aren't well-defined terms. Hex is a notation for writing down or printing out a number (or representing it as a string in memory) but it isn't usually needed in the course of a computation. What I think you're asking is just how to combine two numbers that represent the 16-bit halves of a 32-bit word.
> >
> > If you read in the first column of your file to c1, and the second column to c2, then, if I've understood your question correctly, you can combine them just by doing:
> >
> > c = c1 * 65536 + c2;
> >
> > or, perhaps more clearly,
> >
> > c = bitor( bitshift(c1, 16), c2 );
> >
> > and not using hex at all.
> >
> > Hope that helps.
>
> I have the same problem. Thanks alot David. But, what if one of the values is negative number. In this case, could you please suggest me a solution ? It would be really helpful.
>
> Thank You

You will need to put in a conditional statement (if/else). If the signed bit is the leftmost bit in a 16 bit string, then if your decimal value is greater than (2^15)-1 (or 32767), then the value is negative, or positive, however the signed bit works. You can then use the conditional statement to make the appropriate calculation to make your decimal value negative.

Subject: concatenate?

From: Godzilla

Date: 7 Nov, 2012 00:27:07

Message: 5 of 5

"Wyatt" wrote in message <k7bb0o$f78$1@newscl01ah.mathworks.com>...
> "Yadunandan " <nandu@mando.com> wrote in message <jfsu4b$4b0$1@newscl01ah.mathworks.com>...
> > "David Young" wrote in message <h9b15q$mgf$1@fred.mathworks.com>...
> > > Your questions isn't entirely clear, because "hex number" and "decimal number" aren't well-defined terms. Hex is a notation for writing down or printing out a number (or representing it as a string in memory) but it isn't usually needed in the course of a computation. What I think you're asking is just how to combine two numbers that represent the 16-bit halves of a 32-bit word.
> > >
> > > If you read in the first column of your file to c1, and the second column to c2, then, if I've understood your question correctly, you can combine them just by doing:
> > >
> > > c = c1 * 65536 + c2;
> > >
> > > or, perhaps more clearly,
> > >
> > > c = bitor( bitshift(c1, 16), c2 );
> > >
> > > and not using hex at all.
> > >
> > > Hope that helps.
> >
> > I have the same problem. Thanks alot David. But, what if one of the values is negative number. In this case, could you please suggest me a solution ? It would be really helpful.
> >
> > Thank You
>
> You will need to put in a conditional statement (if/else). If the signed bit is the leftmost bit in a 16 bit string, then if your decimal value is greater than (2^15)-1 (or 32767), then the value is negative, or positive, however the signed bit works. You can then use the conditional statement to make the appropriate calculation to make your decimal value negative.

Since hex2num operates on string, then what about doing

hex2num(['4230' '2FE1'])

Tags for 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