www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - My new least favorite one-liner...

reply Jude Young <10equals2 gmail.com> writes:
--f46d043891bf36a75704b0f89f7e
Content-Type: text/plain; charset=ISO-8859-1

icon = *(toStringz(text(num)));

icon is a char, num is an integer.
I don't suppose there is an easier way to do this?

It's too late and ma brains is mushy.

--f46d043891bf36a75704b0f89f7e
Content-Type: text/html; charset=ISO-8859-1

icon = *(toStringz(text(num)));<br><br>icon is a char, num is an integer.<br>I
don&#39;t suppose there is an easier way to do this?<br><br>It&#39;s too late
and ma brains is mushy.<br>

--f46d043891bf36a75704b0f89f7e--
Nov 05 2011
next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sat, 05 Nov 2011 10:29:36 +0200, Jude Young <10equals2 gmail.com> wrote:

 icon = *(toStringz(text(num)));

 icon is a char, num is an integer.
 I don't suppose there is an easier way to do this?

 It's too late and ma brains is mushy.

If I understood the problem correctly: icon = text(num)[0]; -- or -- icon = cast(char)('0' + num); -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Nov 05 2011
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Jude Young:

 icon = *(toStringz(text(num)));
 
 icon is a char, num is an integer.

Are you trying to convert a single-digit number? import std.stdio; void main() { int x = 5; // in [0 .. 10] char c = cast(char)(x + '0'); writeln(c); } Bye, bearophile
Nov 05 2011
parent Dejan Lekic <dejan.lekic gmail.com> writes:
Regan Heath wrote:

 You've also got std.ascii.digits which is "0123456789" and
 std.string.digits which is an alias of it, so you can say:
 
 import std.ascii; (or std.string)
 
 int x = 5;
 char c = std.ascii.digits[x];
 

I used similar solution to bearophile's before. I must admit i did not know about std.ascii.digits[], thanks for the info Regan.
Nov 07 2011
prev sibling next sibling parent Jude Young <10equals2 gmail.com> writes:
--bcaec53d584b0f42e604b109867c
Content-Type: text/plain; charset=ISO-8859-1

Nice.  Exactly what I was looking for.
I knew I was missing something tiny.

Now I just need to figure out why that works and I can say I've learned
something!
Thanks guys,
Jude

On Sat, Nov 5, 2011 at 5:38 AM, bearophile <bearophileHUGS lycos.com> wrote:

 Jude Young:

 icon = *(toStringz(text(num)));

 icon is a char, num is an integer.

Are you trying to convert a single-digit number? import std.stdio; void main() { int x = 5; // in [0 .. 10] char c = cast(char)(x + '0'); writeln(c); } Bye, bearophile

--bcaec53d584b0f42e604b109867c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Nice.=A0 Exactly what I was looking for.<br>I knew I was missing something = tiny.<br><br>Now I just need to figure out why that works and I can say I&#= 39;ve learned something! <br>Thanks guys,<br>Jude<br><br><div class=3D"gmai= l_quote"> On Sat, Nov 5, 2011 at 5:38 AM, bearophile <span dir=3D"ltr">&lt;<a href=3D= "mailto:bearophileHUGS lycos.com">bearophileHUGS lycos.com</a>&gt;</span> w= rote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde= r-left:1px #ccc solid;padding-left:1ex;"> Jude Young:<br> <div class=3D"im"><br> &gt; icon =3D *(toStringz(text(num)));<br> &gt;<br> &gt; icon is a char, num is an integer.<br> <br> </div>Are you trying to convert a single-digit number?<br> <br> import std.stdio;<br> void main() {<br> =A0 =A0int x =3D 5; // in [0 .. 10]<br> =A0 =A0char c =3D cast(char)(x + &#39;0&#39;);<br> =A0 =A0writeln(c);<br> }<br> <br> Bye,<br> <span class=3D"HOEnZb"><font color=3D"#888888">bearophile<br> </font></span></blockquote></div><br> --bcaec53d584b0f42e604b109867c--
Nov 05 2011
prev sibling parent "Regan Heath" <regan netmail.co.nz> writes:
On Sun, 06 Nov 2011 04:39:28 -0000, Jude Young <10equals2 gmail.com> wrote:
 Nice.  Exactly what I was looking for.
 I knew I was missing something tiny.

 Now I just need to figure out why that works and I can say I've learned
 something!
 Thanks guys,
 Jude

 On Sat, Nov 5, 2011 at 5:38 AM, bearophile <bearophileHUGS lycos.com>  
 wrote:

 Jude Young:

 icon = *(toStringz(text(num)));

 icon is a char, num is an integer.

Are you trying to convert a single-digit number? import std.stdio; void main() { int x = 5; // in [0 .. 10] char c = cast(char)(x + '0'); writeln(c); }


You've also got std.ascii.digits which is "0123456789" and std.string.digits which is an alias of it, so you can say: import std.ascii; (or std.string) int x = 5; char c = std.ascii.digits[x]; -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Nov 07 2011