Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FrojoS · Sep 27, 2012 at 09:05 AM · c#convertboolchar

Convert bool to char in C#

Hi I want to convert a bool to a char. The standard method in C# seems [1] to be

 bool vIn = true;
 char vOut = Convert.ToChar(vIn);

But for me Unity3D produces the following error:

 System.Convert.ToChar (Boolean value)
 InvalidCastException: This conversion is not supported.

I am using the System namespace of course. Thanks for any help. I am a C#/Unity3D rookie.

[1] http://www.convertdatatypes.com/Convert-bool-to-char-in-CSharp.html

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jbevain · Sep 27, 2012 at 09:51 AM 0
Share

@FrojoS, it says that the conversion is not supported because there's no natural conversion from a boolean to an unicode character. What do you think the result should be ? Convert true to '1' and false to '0' ? Convert true to 't' and false to 'f' ? Both don't really make sense. The real question here is, what do you want to do with the resulting char?

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Rnd-- · Sep 27, 2012 at 10:09 AM

try this

 char vOut = (vIn == true ? '1' : '0');
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 27, 2012 at 11:40 AM 1
Share

I'm almost sure that's the most efficient way ;) Oh, btw: You don't have to compare a bool to true. It is already a boolean expression itself ;)

 char vOut = (vIn ? '1' : '0');
avatar image
0

Answer by DocteurCox · Sep 27, 2012 at 09:15 AM

Did not know that bool.toString() didn't exist but whatever. I guess you could just create a simple static method to parse boolean to string :

 static string BoolToString(bool b)
 {
 return b ? "true":"false";
 }

Just put it in an Util class and you would just have to do Util.BoolToString(b)

Hope it helped ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebstar · Sep 27, 2012 at 09:40 AM 2
Share

Hi, he want convert to char, no to string.

@FrojoS: I you want to convert to string use vIn.ToString(). This function exist and you become "true" or "false" as result.

avatar image DocteurCox · Sep 27, 2012 at 10:17 AM 0
Share

oops, didn't read properly :x Well he can just use my method with a char return type and the values he wants.

avatar image Bunny83 · Sep 27, 2012 at 11:37 AM 0
Share

@DocteurCox: Do you have a Java background? ;) because Java uses lower camel case while in C# you usually use upper camel case. In Java there's the function toString in C# / .NET it's ToString

Ohh, of course Java is not UnityScript (Unity's Javascript)

avatar image DocteurCox · Sep 27, 2012 at 01:20 PM 0
Share

Yup, I learned program$$anonymous$$g with Java :)

avatar image
0

Answer by sebstar · Sep 27, 2012 at 09:36 AM

Hi,

try this:

bool vIn = false; char vOut = (char)Convert.ToByte(vIn);

this runs for me.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Convert a char to int / float 2 Answers

I can't figure out why this if statement isn't responding 1 Answer

Enemy spawn, round based 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges