473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Byte to 0 and 1 String

Greetings,

Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

I use:
Integer.toBinaryString((int)readedBytes.get(i))

It returns some odd data like:
11111111111111111111111111011011

It should be returning 8 character ? maybe I am wrong..

Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

Thanks again!
Sep 27 '07 #1
3 1983
Nepomuk
3,112 Expert 2GB
Greetings,

Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

I use:
Integer.toBinaryString((int)readedBytes.get(i))

It returns some odd data like:
11111111111111111111111111011011

It should be returning 8 character ? maybe I am wrong..

Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

Thanks again!
Sure there are known ways - one (probably the wanted way) would be to do manually. You know, where the 1s and 0s come from? Say you have the Byte 9. How do you get the Binary representation (if you want 8 characters: 00001001) from that?

It's no different from what you would do manually - you just have tell your computer how to do it.

Greetings,
Nepomuk

PS.: System.out.println(Integer.toBinaryString(9)); will only give you 1001 as 0s at the front are ignored.
Sep 27 '07 #2
JosAH
11,448 Expert 8TB
Greetings,

Now that i've solver my problem writting a string made of 0 and 1 to a byte. I need to do the reverse process.

I use:
Integer.toBinaryString((int)readedBytes.get(i))

It returns some odd data like:
11111111111111111111111111011011

It should be returning 8 character ? maybe I am wrong..

Anyway, is there any known way to convert a Byte into a string containing a bunch 0 and 1.

Thanks again!
Bytes, shorts, ints and longs are all signed; e.g. -1 is represented as a byte as
11111111; as 1111111111111111 as a short etc. This is the 'sign extension'
mechanism. Simply take the last eight characters from the String representation
and you're in business. Or alternatively chop off all those 'generated' bits by using
this:

Expand|Select|Wrap|Line Numbers
  1. byte b= -1;
  2. String rep= Integer.toBinaryString(b&0xff);
  3.  
kind regards,

Jos
Sep 27 '07 #3
Thanks everyone for the reply :)
Sep 27 '07 #4

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

Similar topics

3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: Jose Perez | last post by:
Dear All, I have the following problem. I am passing an encrypted value on the querystring from one aspx to another. Originally I was outputing characters such as "+, /,..." (which caused...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
10
by: Danny | last post by:
I am working on a project where I will receive xml documents from clients machines as a byte array. They will use the web browser navigate method to post the data to my ASP.NET page. I then pick up...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
10
by: ShadowLocke | last post by:
I'm looking for a better understanding of whats going on with the code i write. I have come across something that I thought I understood but its not working as expected. Its hard to explain so let me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.