Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Casting String to Uint

Explorer ,
Nov 26, 2008 Nov 26, 2008
Hello,

Does anyone know how to cast a string into a uint?

I have tried some along the lines of the following:

var myStr:String = "0x000000";
var myUint:uint;

myUint = uint(myStr);

But i keep getting an error stating that there is a mismatch in variable types or i get a value of '65280' when I trace myUint. I understand that uint is a positive integer but i need to accept a string for the colour value and need to convert it to a string for use.

Any suggestions and correct answers are welcome. I will post or indicate the working solution once this problem has been solved.

Thanks in advance to everyone,

Lee
TOPICS
ActionScript
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Nov 26, 2008 Nov 26, 2008
The uint equivalent of the hex value 0x000000 is 0.

Try this:

var s1:String = "0xffeedd";
trace(s1); //outputs 0xffeedd

var u:uint = uint(s1);
trace(u); // outputs 16772829

var s2:String = u.toString(16);
trace(s2); // outputs ffeedd

var s3:String = "0x" + s2;
trace(s3); // outputs 0xffeedd


The u is the numerical equivalent of the first string.

The second string converts back to hex and the third sticks the "0x" back on. Basically it's all the same value using a different system for reco...
Translate
Explorer ,
Nov 26, 2008 Nov 26, 2008
var s:String = "0x123456";
var u:uint = uint(s);
trace(u); // outputs 1193046


Should work.

Are you sure there's nothing else in there that is causing the problem?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 26, 2008 Nov 26, 2008
Hi,

thank you for your reply. i have followed your instructions and that works but the trace value is not what I want. I am wanting to convert a string with the value of 0x000000 to a uint with a value of 0x000000 not with an obscure value. Its basically to change a colour fill for a shape.

If you are able to expand on this, it would be very helpful. I have read through the uint class and tried several things but had no luck and I cant seem to find anything which gives what I would like on Google or other reputable ActionScript 3.0 websites.

Thanks

Lee
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 26, 2008 Nov 26, 2008
The uint equivalent of the hex value 0x000000 is 0.

Try this:

var s1:String = "0xffeedd";
trace(s1); //outputs 0xffeedd

var u:uint = uint(s1);
trace(u); // outputs 16772829

var s2:String = u.toString(16);
trace(s2); // outputs ffeedd

var s3:String = "0x" + s2;
trace(s3); // outputs 0xffeedd


The u is the numerical equivalent of the first string.

The second string converts back to hex and the third sticks the "0x" back on. Basically it's all the same value using a different system for recording it.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 26, 2008 Nov 26, 2008
There is nothing obscure about what the uint value of a hexadecimal value is--you will not get a uint value to be a hexadecimal value, just the integer value equal to the hexadecimal vaue. Have you tried using the value it does produce for the colorfill?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 23, 2009 Mar 23, 2009
I am having the same problem. The drawing API needs a uint. Because people often pass #000000 instead of 0x000000 and casting a string to a uint simply doesn't work for the drawing API, this functionality is ambiguous.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 23, 2009 Mar 23, 2009
LATEST
yeah ... but you can use string methods to check for different rgb formats and convert. i.e.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines