473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert String To Integer in Javascript

Yanskopolis
9 New Member
Hi, I'm trying to find a more succinct way to transform a string variable into an integer. This is my code I have at the moment:

Expand|Select|Wrap|Line Numbers
  1. var str = "rgb(51, 45, 32)"
  2.  
  3. var first = str.slice(4,6);
  4. var second = str.slice(8,10);
  5. var third = str.slice(12,14);
  6.  
  7. var integOne = parseInt(first); 
  8. var integTwo = parseInt(second);  
  9. var integThree = parseInt(third);
  10.  
  11. ctx.fillStyle = "rgba(integOne, integTwo, integThree, 0.5)";
...which works fine, but I'm pretty sure there must be a more efficient way to go about it.

I tried the following, but without success:

Expand|Select|Wrap|Line Numbers
  1. var str = "rgb(51, 45, 32)";
  2. var strSliced = str.slice(4,14);
  3. eval("ctx.fillStyle = 'rgba("+strSliced+", 0.5)'");
Is there a better way to do this?

Cheers.
Mar 20 '07 #1
4 10258
Yanskopolis
9 New Member
Hmm, seems I got a bit mixed up as I didn't need to convert it to an integer anyway.

Mods can close/delete this thread if you want. :)
Mar 20 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
Hmm, seems I got a bit mixed up as I didn't need to convert it to an integer anyway.

Mods can close/delete this thread if you want. :)
Yes, if you needed to manipulate the numbers in some way, then maybe, but since you're copying, it wasn't necessary.

I'll keep this thread. It might prove useful to someone.
Mar 20 '07 #3
dmjpro
2,476 Top Contributor
what's the error u getting????
Mar 20 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
what's the error u getting????
If you read the 2nd post, the problem has been solved.
Mar 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
9
by: mprocopio | last post by:
Fellow JavaScripters, I am looking for code or implementation ideas for converting an integer variable to a four-byte array. I'm porting some of my code from C#, where I make use of their...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb...
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: Lonifasiko | last post by:
Hi, I want to convert an UTC time to a Date object in Javascript. The UTC time we have is a string that looks like "1160720058.377452373" for example. I've done it in Java but I'm not able...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.