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
2
Question by benk0913 · Jun 17, 2012 at 11:06 AM · c#floatintconvertchar

Convert a char to int / float

A simple question, how do I convert a char (in this case it comes from a string (content[0])) right in to int or float?

So if the char shows '4', the ints value whould be 4.

Many thanks! :)

Comment
Add comment · Show 2
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 Jessy · Jun 17, 2012 at 03:18 PM 2
Share

This is not a good site for non-Unity questions; you'll find many more knowledgeable programmers at Stack Overflow.

avatar image Mizuho · Jun 17, 2012 at 11:57 PM 0
Share

I use int.Parse() and float.Parse(). It's $$anonymous$$ORE advised, however, that you use int.TryParse() and float.TryParse() unless you are absolutely convinced that you will never have an illegal value enter that variable.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by fafase · Jun 17, 2012 at 11:13 AM

http://msdn.microsoft.com/en-us/library/system.single.tryparse.aspx

Have a look there

It goes like this with Js:

 var str:String  = "5";
 var fl:float;
 function Update(){
    float.TryParse(str,fl);
    fl *= 2;
    Debug.Log(fl); // Prints out 10
 }

Edit: As required by Benk:

 var str:String  = "5 and other letters";
 var integer :int;
     function Update(){
        integer = str[0];
        integer -=48;
        Debug.Log(integer); // Prints out 5
     }

The -48 is because 0 is 48 (0x0030) in Unicode value and you need to subtract that value to get the integer representation.

Comment
Add comment · Show 6 · 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 Berenger · Jun 17, 2012 at 04:46 PM 0
Share

benk0913 : I'm talking about a char variable not a string variable.

avatar image fafase · Jun 17, 2012 at 07:53 PM 0
Share

Ok, so first off, if you want to convert a char variable, you won't be able to get a float but only a int. char only holds 128 values (ASCII table) and does are all 1 byte only so no possible float. Now, you can get the ASCII value of your char as I show in the edit of my answer.

avatar image whydoidoit · Jun 17, 2012 at 08:05 PM 0
Share

Well you can get a float, just it won't have any fractional parts.

avatar image whydoidoit · Jun 17, 2012 at 08:28 PM 0
Share

@fafase - you're right, have deleted my answer, the function was ToInt32(myChar) but it complains the cast doesn't work anyway! $$anonymous$$gest if you want a float to convert your int to a float.

avatar image Eric5h5 · Jun 17, 2012 at 11:54 PM 0
Share

@fafase: char doesn't use ASCII, it's Unicode. It holds 65$$anonymous$$+ values, and it's not a byte.

Show more comments
avatar image
1

Answer by Setzer22 · Jun 17, 2012 at 04:33 PM

The .NET method to do that is:

Char.GetNumericValue(randomChar) //randomChar is the char you want to convert

I don't know if this works on UnityScript (assuming it uses the same libraries as C# does, I'll say it should work).

Also, I don't think this is needed, but if this doesn't work, try adding "using System;" (without brackets) at the top of your script.

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 Setzer22 · Jun 17, 2012 at 04:36 PM 0
Share

This works on a Char itself, not an string, which is an array of chars. Although reading chars it's not what you normally want, because a char only represents an alphanumeric value (so, it can only represent the numbers from 0 to 9, you cannot have a char containing 10 or 12).

It's because of that that normally, you want to add some numeric chars into a string so you can convert numbers being greater than 9 into integer variables.

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

8 People are following this question.

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

Related Questions

Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 1 Answer

Convert Text to float 3 Answers

Turn float to int 2 Answers

Convert bool to char in C# 3 Answers

Convert string to int C# 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