Jump to content

How can I convert the content in a csv from a int to boolean ?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
I able to convert 1 of the int to boolean but not all ... Who can help ?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
something like:

mybool = (myint != 0)

?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

WingedPanther said:

something like:

mybool = (myint != 0)

?

Or !!myint if it is allowed.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
yea I try that ... but the content in my csv file is 1110000100 ... I want the result to be true true true false false false etc ... not just true only ...

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
?????? Any pro can help ?????

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Assuming you're using 32 bit integers:

// Integer containing values to test is assumed to be called 'myint'

final int maxbits = 32;

boolean[] mybools = new boolean[maxbits];

for (int i = 0; i < maxbits; i++) {

    mybools[i] = (myint & Math.pow(2, i)) != 0;

}


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#7
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Why convert to int when you can use the string. Just loop through every character in the string, you save yourself the conversion


		String content = "11110101011101101";

		int len = content.length();

		boolean[] bools = new boolean[len];

		for (int i = 0; i < len; i++) {

			if (content.charAt(i) == '0')

				bools[i] = true;

			else

				bools[i] = false;

		}


And then you can read them with

for (boolean b: bools)

    System.out.println(b);






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users