www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Convert int to dchar

reply Paul <phshaffer gmail.com> writes:
    I'm sure I'm making this more difficult than it needs to be.  
I'm trying to convert an integer to a dchar.  The solution below 
works but seems like overkill.

     dstring dstrValue = to!dstring(5);
     dchar dcharValue = to!dchar(dstrValue);

... this,

     dchar dcharValue = to!dchar(5);

... writes out '\x005' ..or something close to that.
Oct 05 2022
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/5/22 12:57 PM, Paul wrote:
     I'm sure I'm making this more difficult than it needs to be. I'm 
 trying to convert an integer to a dchar.  The solution below works but 
 seems like overkill.
 
      dstring dstrValue = to!dstring(5);
      dchar dcharValue = to!dchar(dstrValue);
 
 ... this,
 
      dchar dcharValue = to!dchar(5);
 
 ... writes out '\x005' ..or something close to that.
dchar dstrValue = '5'; -Steve
Oct 05 2022
parent Paul <phshaffer gmail.com> writes:
Thanks Steve.  I need to covert something like this:
     int myvar = 5;
How would I convert myvar to a dchar?
Oct 05 2022
prev sibling parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Wed, Oct 05, 2022 at 04:57:57PM +0000, Paul via Digitalmars-d-learn wrote:
    I'm sure I'm making this more difficult than it needs to be.  I'm
 trying to convert an integer to a dchar.  The solution below works but
 seems like overkill.
 
     dstring dstrValue = to!dstring(5);
     dchar dcharValue = to!dchar(dstrValue);
 
 ... this,
 
     dchar dcharValue = to!dchar(5);
 
 ... writes out '\x005' ..or something close to that.
What exactly do you mean by "convert an integer to a dchar"? Do you mean converting an integer between 0 and 9 into a dchar representing its digit value, or do you mean creating a dchar containing the unicode code point represented by the int? For the former: dchar ch = '0' + intValue; (Though you will have to consider what should happen if intValue > 9.) For the latter: dchar ch = cast(dchar) intValue; T -- Those who've learned LaTeX swear by it. Those who are learning LaTeX swear at it. -- Pete Bleackley
Oct 05 2022
parent Paul <phshaffer gmail.com> writes:
On Wednesday, 5 October 2022 at 17:16:29 UTC, H. S. Teoh wrote:

 For the former:

 	dchar ch = '0' + intValue;
This! Thanks Teoh.
Oct 05 2022