Convert variable into an array
I have variable, abc; whose value is in the format val1,val2,val3,..
I want to convert it into an array, like that arr[0]=val1; arr[1]=val2; so on. How can I do this?
Usman
examine this class that I did
import java.lang.*;
public class ReadString {
public static void main(String[] args){
String abc = "1,2,3,4,5,6";
StringBuffer abc2 = new StringBuffer(abc);
int arrayLength = abc2.length(); //get length
int[] abcFinal = new int[6];
for (int i=0; i<arrayLength; i++) { //test characters
int anIndex;
if (!(abc2.substring(i,i+1)).equals(",")) { //if it is a number
//(i/2) is ta little formula to be able to assign the
//right index in the intArray to the number
anIndex = (i/2);
//set value to array
abcFinal[anIndex] = Integer.parseInt(abc2.substring(i,i+1));
for (int j=0;j<abcFinal.length;j++ ) {
System.out.println("i = " + j + " value = " + abcFinal[j]);
}//end main
}//end class
Similar Messages
-
Converting image into byte array
Hi all,
How to convert an integer array image into byte array ?
here i have a image like this :
private static int pixelArray[] = {
0xff000000, 0xff000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
0xff000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000
From this one i create image like this one :
Image image = Image.createRGBImage(pixelArray, width, height, true);
Now i want to convert that pixelArray into byte array ? how to do this
additionally i want to send this byte array to servlet .
thanks in advance.Hi,
If you want to convert your int array to a byte array you should
split each integer into 4 bytes to avoid losing information. You can
rebuild your integer array later if you need to. I think this code
will work (i havent tested it):
byte[] pixel= new byte[pixelArray.length<< 2];
for (int i= pixelArray.length- 1; i>= 0; i--) {
int aux= i<< 2; // i* 4 = i<< 2
pixel[aux]= (byte) (pixelArray>> 32);
pixel[aux+ 1]= (byte) (pixelArray[i]>>> 16);
pixel[aux+ 2]= (byte) (pixelArray[i]>>> 8);
pixel[aux+ 3]= (byte) pixelArray[i];
Greets. -
Put loop of variables into single array with formatting (PHP)
I have the following loop;
<?php
$i=1;
while($i<=$num_rows)
$tempquantity = "extra".$i."quantity";
$tempcomments = "extra".$i."comments";
$tempname = "extra".$i."name";
$tempprice = "extra".$i."price";
$$tempquantity = $_POST['extra' . $i . 'quantity'];
$$tempcomments = $_POST['extra' . $i . 'comments'];
$$tempname = $_POST['extra' . $i . 'name'];
$$tempprice = $_POST['extra' . $i . 'price'];
print "Extra name: " . $$tempname;
print "<br />";
print "Extra price: " . $$tempprice;
print "<br />";
print "Quantity required: " . $$tempquantity;
print "<br />";
print "Client comments: " . $$tempcomments;
print "<br /><br />";
$i++;
?>
Which, since there are 3 rows, outputs the following;
Extra name: dfvgfddf
Extra price: 4
Quantity required: 67
Client comments: gtfh
Extra name: wewew
Extra price: 34
Quantity required: 45
Client comments: thtrt
Extra name: ewewe
Extra price: 43
Quantity required: 12
Client comments: gdfgg
What I want to be able to do is show the output above in a PHP email body variable, which I think this means that the loop of varaibles needs to be in one single array variable since coding in an email body variable is not allowed. How do I put a loop of variables like this into a single variable, and how to I keep the <br /> tags and names in front of the varaibles ('Extra name:' etc), so it looks as it should in the PHP email body?
Thanks in advanceHi Murray,
This is the code that sends the email;
$to = "$renteremail";
$subject = "Request confirmation";
$headers = "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: website";
$body = "
Your reference number is <strong>$rrnumber</strong>.
<br />
<br />
Below is a summary of your request details;
<br /><br /><strong>Accommodation name</strong>: $accomm_name
<br /><strong>Accommodation type</strong>: $accomm_type
<br /><strong>Board-type</strong>: $ptbt
<br /><strong>Max. capacity</strong>: $ptc
<br /><strong>Check-in date</strong>: $passdate
<br /><strong>Number of nights</strong>: $passnights
<br /><br /><strong>Renter's name</strong>: $rentername
if (mail($to, $subject, $body, $headers)) {$messagesent = 'yes';}
So the three loop items with their four respective variables should go at the end of the list of variables above after 'Renters name'.
I also need to put the variables generated by the loop into a single database feild, so if possible all should go into one variable.
Thanks -
Is there an easier way to convert bytes into bit(boolean) arrays?
I am currently using this method to convert bytes into bit arrays:
/*convert byte to int such that it is between 0-255 this is the bytes[] array
if ((bytes/128)==1)
bit[c+0]=true;
else
bit[c+0]=false;
if ((bytes[i]-bitInt[c+0]*128)/64==1)
bit[c+1]=true;
else
bit[c+1]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64)/32==1)
bit[c+2]=true;
else
bit[c+2]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32)/16==1)
bit[c+3]=true;
else
bit[c+3]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16)/8==1)
bit[c+4]=true;
else
bit[c+4]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8)/4==1)
bit[c+5]=true;
else
bit[c+5]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4)/2==1)
bit[c+6]=true;
else
bit[c+6]= false;
if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4-bitInt[c+6]*2)==1)
bit[c+7]=true;
else
bit[c+7]= false;You can loop through and use a bitwise operator instead. Here is an example without the loop.
byte b = 6 ;
if( b & Math.pow( 2, 0 ) == Math.pow( 2, 0 ) ) ;
// the 2^0 bit is on
if( b & Math.pow( 2, 1 ) == Math.pow( 2, 1 ) ) ;
// the 2^1 bit is onetc...
You should get something like 110 when you're done.
If you're wonder what & does (no, its not a boolean &&), it takes the bits in the two numbers you give it and returns a number with all the bits on that are on for each of them.
For example:
10011011 &
11001101 =
10001001
So if we take
110 (6) &
010 (2^1, or 2) =
010 (2 again)
Which means that the number (6) has the 2^1 bit on. -
What is the best way to convert a cluster into byte array or string
I'm writing a program that sends UDP packets and I've defined the data I want to send via large clusters (with u8/u16/u32 numbers, u8/u16/u32 arrays, and nested clusters). Right before sending the data, I need to convert the clusters either into strings or byte arrays. The flatten to string function is almost perfect for this purpose. However, it's appending lengths to arrays and strings which renders this method useless, as far as I can tell.
As I have many of these clusters, I would rather not hard code the unbundle by names and converting/typecasting to byte arrays or strings for each one.
Is there a feature or tool I am overlooking?
Thank you!deceased wrote:
Flatten to string has a boolean input of "Prepend string or array size" ... The default value is true.
That only specifies if a string or array size should be prepended if the outermost data element is a string or array. For embedded strings or arrays it has no influence. This is needed for the Unflatten to be able to reconstruct the size of the embedded strings and arrays.
The choice to represent the "Strings" (and Arrays) in the external protocol to LabVIEW strings (and arrays) is actually a pretty bad one unless there is some other element in the cluster that does define the length of the string. An external protocol always needs some means to determine how long the embedded string or array would be in order to decode the subsequent elements that follow correctly.
Possible choices here are therefore:
1) some explicit length in the protocol (usually prepended to the actual string or array)
2) a terminating NULL character for strings, (not very friendly for reliable protocol parsing)
3) A fixed size array or string
For number 1) and 2) you would always need to do some special processing unless the protocol happens to use explicitedly 32 bit integer length indicators directly prepended before the variable sized that.
For number 3) the best representation in LabVIEW is actually a cluster with as many elements inside as the fixed size.
-
Hello,
in my vi I have a chart with 5 Plots displaying measurement data.
The user should be able to save all the history data from the chart at anytime. (e.g. the user watches the chart and some event happens, then he presses a "save"-button)
I know, that I can read out the history data with a property node. That is not the problem. The problem is, how do I handle the data? The type of the history data is a 1-D array of cluster of 5 elements.
I have to convert that data somehow into a 2 D-array of numbers or strings, so that I can easily save it in a text-file.
How do I convert a 1-D array of cluster of 5 elements into a 2-D array of numbers?
I use LabVIEW 7.1
Johannes
Greetings Johannes
Using LabVIEW 7.1 and 2009 recently
Solved!
Go to Solution.Gerd,
thank you for the quick response and the easy solution.
Look what I did in the meantime. I solved the problem too, but muuuch more complicate :-)
And I have converted the numbers to strings, so that I can easily write them into a spreasheet file.
Johannes
Message Edited by johanneshoer on 04-28-2009 10:39 AM
Greetings Johannes
Using LabVIEW 7.1 and 2009 recently
Attachments:
SaveChartHistory.JPG 57 KB
SaveChartHistory.JPG 57 KB -
Converting object wrapper type array into equivalent primary type array
Hi All!
My question is how to convert object wrapper type array into equivalent prime type array, e.g. Integer[] -> int[] or Float[] -> float[] etc.
Is sound like a trivial task however the problem is that I do not know the type I work with. To understand what I mean, please read the following code -
//Method signature
Object createArray( Class clazz, String value ) throws Exception;
//and usage should be as follows:
Object arr = createArray( Integer.class, "2%%3%%4" );
//"arr" will be passed as a parameter of a method again via reflection
public void compute( Object... args ) {
a = (int[])args[0];
//or
Object arr = createArray( Double.class, "2%%3%%4" );
public void compute( Object... args ) {
b = (double[])args[0];
//and the method implementation -
Object createArray( Class clazz, String value ) throws Exception {
String[] split = value.split( "%%" );
//create array, e.g. Integer[] or Double[] etc.
Object[] o = (Object[])Array.newInstance( clazz, split.length );
//fill the array with parsed values, on parse error exception will be thrown
for (int i = 0; i < split.length; i++) {
Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
o[i] = meth.invoke( null, new Object[]{ split[i] });
//here convert Object[] to Object of type int[] or double[] etc...
/* and return that object*/
//NB!!! I want to avoid the following code:
if( o instanceof Integer[] ) {
int[] ar = new int[o.length];
for (int i = 0; i < o.length; i++) {
ar[i] = (Integer)o;
return ar;
} else if( o instanceof Double[] ) {
//...repeat "else if" for all primary types... :(
return null;
Unfortunately I was unable to find any useful method in Java API (I work with 1.5).
Did I make myself clear? :)
Thanks in advance,
Pavel GrigorenkoI think I've found the answer myself ;-)
Never thought I could use something like int.class or double.class,
so the next statement holds int[] q = (int[])Array.newInstance( int.class, 2 );
and the easy solution is the following -
Object primeArray = Array.newInstance( token.getPrimeClass(), split.length );
for (int j = 0; j < split.length; j++) {
Method meth = clazz.getMethod( "valueOf", new Class[]{ String.class });
Object val = meth.invoke( null, new Object[]{ split[j] });
Array.set( primeArray, j, val );
}where "token.getPrimeClass()" return appropriate Class, i.e. int.class, float.class etc. -
How to convert an int variable into String type
hi everybody
i want to know how to convert an interger variable into string variable
i have to implement a code which goes like this
Chioce ch;
for(int i=0;i<32;i++)
// here i need a code to convert the int variable i into a string variable
ch.add(String variable);
how do i convert that int variable i into a String type variable??
can anyone help me?Different methods:
int a;
string s=a+"";or
String.valueOf(int) is the better option because Int.toString() generated an intermediate object to get the endresult
Ema -
Loading a map + variables "drawn" in a txt file into an array
hey guys, somewhat new to java, i have an assignment for school that requires me to create an "AI" that can solve a maze, easy enough. I used a 2 dimensional array for the maze, where 1 = wall, 0 = path, 3 = visited. etc, as far as the navigating goes, it's all done. The problem: i need to be able to load a maze from a file, the file looks like this:
7 20 <-- rows/columns
0 10 <------ start
6 5 <---- finish
xxxxxxxxxx xxxxxxxxxx
x x xxx x
x xxxxxx xxx xxx
x xxxxxxx xxxx xxxxxx
x x xx xxx
x xx xxxxxxx xx x
xxxxxoxxxxxxxxxxxxxxxSo basically i need to store the first numbers in variables so i know what size array to create and where to start solving the maze and then i somehow need to figure out a way to transofrm that drawing into an array. Was looking into parsing the text and trying to use tokens to separate it, but i honestly can't figure out how. If someone can drop a few hints or some functions i can use, i'd much appreciate it!Read the Java Doc API for
FileReader
BufferedReader
String.split
String.toCharArray -
How to convert an array of bits into an array of bytes?
If I have an array of 1s and 0s, how can I pack them
into an array of bytes (8 bits)?
Thanks.NI recommends to get rid of 4.x mode (Quote from the help file: "National Instruments recommends reworking any application that uses the Convert 4.x Data mode as a long term solution."). For this reason, I have attached an alternative example to give you some ideas.
(The previous example (using 4.x type cast) is actually slightly flawed, because for some reason it only produces even sized output arrays (why???) so you would need to trim the resulting array as a function of the input array size.)
LabVIEW Champion . Do more with less code and in less time .
Attachments:
OnesAndZeroesU8v2.vi 43 KB -
Converting a string of numbers into an array of booleans
For a homework assignment, I need to convert a string of numbers into an array of booleans. The string is a bunch of random numbers (0-9) in sequence, with no spaces. It looks something like this: 0123452348949230740329817438120947392147809231419. I need to make it so that each even number represents a "True" boolean and so that each odd number represents a "False" boolean. I think that I first need to convert each element of the string into a number, and then use a case structure (or something) to say, for each element of the array, "If even, then true. If odd, then false," but I could be wrong. Any suggestions?
billko wrote:
Hooovahh wrote:
billko wrote:
Sounds reasonable. Think about the definition of "odd" and "even" and it will make life a lot easier.
I know you are trying to be vague but I'd like to give a key hint. Use the Quotient and Remainder function.
LOL maybe that was one of the objectives of the homework.
To be fair it sounds like there is more work that is needed. A new user of LabVIEW will have a hard time figuring out how to process each character one at a time when it isn't in an array.
It's just that most people (me at least) stopped thinking about division with quotient and remainder after basic algebra. I then of course changed my way of thinking when I used LabVIEW. Still most of the time when you use division you want to know the fractional part as a decimal. Thinking this way makes the problem more difficult then it needs to be.
Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
If 10 out of 10 experts in any field say something is bad, you should probably take their opinion seriously. -
How to convert arraylist into array
how to convert arraylist into array?
If you are using generics, I would use this version of toArray:
List < X > list = ...
X[] array = list.toArray(new X[list.size()]);If you are not using generics, that same thing looks like:
List list = ...
X[] array = (X[]) list.toArray(new X[list.size()]); -
Converting data pointed to by a pointer into an array
The vi I use returns a pointer to a block of data. The data are
unsigned 16-bit integers. Without using a for..do loop, how do I
convert the data into an array for input into an intensity graph?
(Note that I presently use a for...do loop that works by incrementing
the pointer by 2 bytes each time. But it takes much time and I
want to eliminate using a loop in labview. I suspect built-in
functions/using DLLs would be much faster. But I do not know how to go
about doing it.)
Thank you for your help.Hi Balakumar,
> The vi I use returns a pointer to a block of data.
If by "pointer" you mean a memory address, then I think you're DLL idea is right on track. You could efficiently cast (sort of) the pointer into an array.
The attached .zip contains some VIs that may give you some ideas. In LabVIEW, open the application builder and load CVTDLL.bld. It compiles CVTDLL.vi into CVTDLL.dll. CVTDLLCaller calls the DLL - passing it the pointer and returning an array. The magic will happen in the way the pointer parameter is passed from the VI to the DLL. I think we need the CLF to think it's supposed to be passing a U32 value, where the DLL thinks it receiving a pointer to an array. Then it's just a matter of passing the data back out. There are a bunch of forum contributors who could get this right the first try - "smercurio" and "RolfK" come to mind.
I hope this isn't yet another case where I'm way off track!
cheers.
When they give imbeciles handicap-parking, I won't have so far to walk!
Attachments:
CVTDLL.zip 20 KB -
Convert variable type f into variable type p decimals 2
Hi,
How can i convert a type f variable into a type p decimals 2 variable?
I'm trying to make this division:
var type f.
var1 type p decimals 2
var = 50 / 100.
the result is:
var = 5.0000000000000000E-01
if i do
move var to var1.
the result is:
var1 = 0.01 ???
can anyone tell me what i'm doing wrong?DATA: var type f,
var1 type p decimals 2.
var = 50 / 100.
WRITE:/ var.
move var to var1.
WRITE:/ var1.
Result is: 0.50
I don't see what's your problem...
Greetings,
Blag. -
I do not have NI-IMAQ or NI Vision.
I acquired the image using a picolo board then just saved it as a bitmap file.You want to convert it to an array of what? Of numbers? Of LabVIEW colors?
The "Read BMP File.vi" VI outputs a 1-D array of the pixels, but I suspect it is not exactly in the format that you need. I am NOT sure, but I think that each group of 3 elements of that 1-D array (which is inside the cluster "image data" that the VI outputs) represents the red, green and blue levels of a single pixel (but it may be hue, saturation and lum.). Also, since it is a 1-D array, you would have to divide it by the width (which is also included in the "image data" cluster) to get some sort of 2-D array of data.
You can find the "Read BMP File.vi" VI in the functions palete> "Graphics & sound">"Graphics Formats".
Maybe you are looking for
-
Why does Lion immediately reboot with FileVault Enabled?
I have two laptops, both with Lion installed and both are updated to the latest patches. Here's the process I go through: Shutdown the laptop Do something else (take a nap, eat a sandwich, whatever) Boot laptop Log in Here I get the little grey pinwh
-
Disable esmtp Inspection for Specific Host
Hello. Is it possible to disable esmtp inspection for a specific INSIDE host with use of a policy-map? If so, could you provide an example configuration.
-
Getting waved-horizontal lines in my imagery just after working in photoshop...sending to files as with after completing some applications, such as printing. Nothing is wrong with my Mac...Issue does not occur every time I go into using Photoshop?
-
Hi Can any one explain me how to setup benefits and plans, Reimbursement. Thanks in advance Nakshathra
-
NEWB: Premier CS4 & video not working
Hi, I've installed CS4 as a trial on an IBM think pad, that's a bit old and video was not playing. The sound was not able to start from different points and video was only a single frame. I figure, time to upgrade anyway's, I shop around and find a