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:
how to replace element of an array with char?

Subject: how to replace element of an array with char?

From: prabhakaran m

Date: 6 Aug, 2013 01:16:09

Message: 1 of 8

Dear all,
 i have written a programme to convert decimal no to hexa.

clear all
r=input('\n enter the decimal number to convert in octal=');
n=1000;
dis=[];
 for i=1:n
quo=floor(r/16)
dis(i)=rem(r,16)
r=quo;

if quo <= 0
break
end
 
    
 if dis(i) == 10
      dis(i)=char('A')
    elseif dis(i)==11
    dis(i)=char('B')
    elseif dis(i)==12
    dis(i)=char('C')
    elseif dis(i)==13
    dis(i)= char('D')
    elseif dis(i) ==14
    dis(i)=char('E')
    elseif dis(i)==15
    dis(i) =char('F')
    
 end
 end
 
 X=fliplr(dis)
disp(sprintf('The equivalent octal number is : %s',num2str(X)))

if i input 45 i should get 2D as answer but i am getting 2 68. matlab shows me a numeric value equivalent to D. how i should ignore it.please help with this.

Subject: how to replace element of an array with char?

From: Roger Stafford

Date: 6 Aug, 2013 02:22:14

Message: 2 of 8

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <ktpiop$mg8$1@newscl01ah.mathworks.com>...
> .......
> if i input 45 i should get 2D as answer but i am getting 2 68. matlab shows me a numeric value equivalent to D. how i should ignore it.please help with this.
- - - - - - -
  Your difficulty is caused by using char('D') for your remainder. The ASCII number for character 'D' is 68, so when 'num2str' converts it again to characters, a '68' is printed out.

  You can add 'A'-10 to your remainders which are 10 or above, instead of handling each of the six cases individually, and then apply a 'char' to all the numbers rather than using 'num2str'. It cannot print letters like 'D' as you had hoped.

  Note: This is conversion to hexadecimal, not octal.

Roger Stafford

Subject: how to replace element of an array with char?

From: Roger Stafford

Date: 6 Aug, 2013 02:33:07

Message: 3 of 8

"Roger Stafford" wrote in message <ktpmkl$14e$1@newscl01ah.mathworks.com>...
> "prabhakaran m" <prabha.gahon@gmail.com> wrote in message <ktpiop$mg8$1@newscl01ah.mathworks.com>...
> > .......
> > if i input 45 i should get 2D as answer but i am getting 2 68. matlab shows me a numeric value equivalent to D. how i should ignore it.please help with this.
> - - - - - - -
> Your difficulty is caused by using char('D') for your remainder. The ASCII number for character 'D' is 68, so when 'num2str' converts it again to characters, a '68' is printed out.
>
> You can add 'A'-10 to your remainders which are 10 or above, instead of handling each of the six cases individually, and then apply a 'char' to all the numbers rather than using 'num2str'. It cannot print letters like 'D' as you had hoped.
>
> Note: This is conversion to hexadecimal, not octal.
>
> Roger Stafford
- - - - - -
  Correction: I meant to say add 'A'-'0'-10 to your remainder.

Roger Stafford

Subject: how to replace element of an array with char?

From: prabhakaran m

Date: 6 Aug, 2013 02:49:09

Message: 4 of 8

"Roger Stafford" wrote in message <ktpn93$2da$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <ktpmkl$14e$1@newscl01ah.mathworks.com>...
> > "prabhakaran m" <prabha.gahon@gmail.com> wrote in message <ktpiop$mg8$1@newscl01ah.mathworks.com>...
> > > .......
> > > if i input 45 i should get 2D as answer but i am getting 2 68. matlab shows me a numeric value equivalent to D. how i should ignore it.please help with this.
> > - - - - - - -
> > Your difficulty is caused by using char('D') for your remainder. The ASCII number for character 'D' is 68, so when 'num2str' converts it again to characters, a '68' is printed out.
> >
> > You can add 'A'-10 to your remainders which are 10 or above, instead of handling each of the six cases individually, and then apply a 'char' to all the numbers rather than using 'num2str'. It cannot print letters like 'D' as you had hoped.
> >
> > Note: This is conversion to hexadecimal, not octal.
> >
> > Roger Stafford
> - - - - - -
> Correction: I meant to say add 'A'-'0'-10 to your remainder.
>
> Roger Stafford

Thank you for your reply. I ll try doing. And sorry, since am writing all conversions i typed wrongly as hexo code.

Subject: how to replace element of an array with char?

From: prabhakaran m

Date: 6 Aug, 2013 08:47:07

Message: 5 of 8

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message
> > > You can add 'A'-10 to your remainders which are 10 or above, instead of handling each of the six cases individually, and then apply a 'char' to all the numbers rather than using 'num2str'. It cannot print letters like 'D' as you had hoped.
> > >

i have did as u said. am little confused with how to apply 'char' to all the numbers. say suppose
if i get my remainders x=[2 68] then char(x) gives only D not 2D. what is the mistake am doing.

Subject: how to replace element of an array with char?

From: Roger Stafford

Date: 6 Aug, 2013 10:46:12

Message: 6 of 8

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <ktqd6b$j4h$1@newscl01ah.mathworks.com>...
> i have did as u said. am little confused with how to apply 'char' to all the numbers. say suppose
> if i get my remainders x=[2 68] then char(x) gives only D not 2D. what is the mistake am doing.
- - - - - - -
  I'm sorry if I misled you. I should have said to do this within the for-loop:

 if dis(i) >= 10
  dis(i) = dis(i) + 'A' - 10;
 else
  dis(i) = dis(i) + '0';
 end

and after the for-loop:

 fprintf('The equivalent hexadecimal number is: %s\n',char(X))

without using 'num2str', which cannot output the hexadecimal characters 'A' through 'F'.

Roger Stafford

Subject: how to replace element of an array with char?

From: prabhakaran m

Date: 6 Aug, 2013 12:55:18

Message: 7 of 8

"Roger Stafford" wrote in message <ktqk5k$4b0$1@newscl01ah.mathworks.com>...
> "prabhakaran m" <prabha.gahon@gmail.com> wrote in message <ktqd6b$j4h$1@newscl01ah.mathworks.com>...
> > i have did as u said. am little confused with how to apply 'char' to all the numbers. say suppose
> > if i get my remainders x=[2 68] then char(x) gives only D not 2D. what is the mistake am doing.
> - - - - - - -
> I'm sorry if I misled you. I should have said to do this within the for-loop:
>
> if dis(i) >= 10
> dis(i) = dis(i) + 'A' - 10;
> else
> dis(i) = dis(i) + '0';
> end
>
> and after the for-loop:
>
> fprintf('The equivalent hexadecimal number is: %s\n',char(X))
>
> without using 'num2str', which cannot output the hexadecimal characters 'A' through 'F'.
>
> Roger Stafford

No you didn't mislead. i did the same but didnt put the else statement. now i got it and thank you so much :)

Subject: how to replace element of an array with char?

From: Steven_Lord

Date: 6 Aug, 2013 16:06:41

Message: 8 of 8



"prabhakaran m" <prabha.gahon@gmail.com> wrote in message
news:ktpiop$mg8$1@newscl01ah.mathworks.com...
> Dear all,
> i have written a programme to convert decimal no to hexa.
> clear all
> r=input('\n enter the decimal number to convert in octal=');

Are you converting to hex or octal?

> n=1000;
> dis=[];
> for i=1:n quo=floor(r/16)
> dis(i)=rem(r,16)

This suggests hex.

> r=quo;
>
> if quo <= 0
> break
> end
>
> if dis(i) == 10
> dis(i)=char('A')

You can't mix characters and numbers in the same array. If you try to assign
a character into a numeric array, the numeric array will store the ASCII
value of that character. If you try to assign a numeric value into a char
array, the char array will convert the numeric value into a character (which
may warn if the number is not a nonnegative integer or is too large, and may
not be a printable character: char(7) is not '7' but is the "bell"
character.)

http://www.asciitable.com/

If I were not allowed to use DEC2HEX to perform this task, I would use
indexing.

% Example
dis = [1 0 11 3 8]
hexDigits = '0123456789ABCDEF';
characterRepresentation = hexDigits(dis+1)

Or of course you could just use DEC2HEX.

dec2hex(68408)

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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