• Post Reply Bookmark Topic Watch Topic
  • New Topic

convert long to string  RSS feed

 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a long value (255) from which I am getting binary value as a string something like 111111

String binary = Long.toBinaryString(str.getLongValue());


Now can we convert this value in to string? means if we have above binary value can we get strings from it like "test" or something else?

Thanks!!!
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not getting your question .. Are you asking about the following line of code? If so this won't compile. Use str.longValue() assuming str is a Long reference.

 
James Basller
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:I'm not getting your question .. Are you asking about the following line of code? If so this won't compile. Use str.longValue() assuming str is a Long reference.



hi

thanks for reply.

but I want to convert binary value into ascii format and from that ascii I want to convert in string format

can I do that?

Thanks!!!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand why you need ASCII characters. That method creates a String directly, but the characters are all Unicode. Have you read the documentation for that method? It tells you what sort of parameters it requires.

By the way, 255 is 11111111 not 111111.
 
angel mahe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like llaffer said you will want sprintf() but the buffer (the char[]) you write to will have to be at least 12 bytes long to avoid overflows as an integer can hold (2^31)-1 to -(2^31), 2^31 is 10 characters long (2147483648) and a negative sign makes 11 characters long and finally you need the null terminator which makes 12 characters/bytes, also as count is an integer and not a real number. e.g.

char buffer[12];
sprintf(buffer,"%d",posTween(/* values here */));
if you are simply going to print the string to the console then you can skip the buffer and use
printf("%d",posTween(/* values here */))
or if you write it to a file then
fprintf(STDOUT,"%d",posTween(/* values here */));
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have difficulty getting that code to work. It doesn't look like Java. It looks like valid C, however
 
Roldan Baldo
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is not specific or vague.

make it simple and understandable, post your code if necessary or an algorithm of the problem.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the OP is asking for a way to convert a String having binary numbers to alphabets

eg:
"0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100"

should be converted to:
"hello world"
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, it's easy:
- split the string into chunks of 8 digits each
- convert each digit to a byte
- wrap those bytes in a ByteArrayInputStream
- wrap that ByteArrayInputStream in an InputStreamReader
- start reading characters!
 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishna bala wrote:

Why not simply use Integer.parseInt(s1, 2)?
 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , we will do it
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!