473,394 Members | 1,932 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.

Binary Conversion , Help please!

7
I need to convert any integer value greate than 127 to binary value using this example:
Represent the value in binary
(e.g 137 => 1000 1001)
2) Break in up in groups of 7 bits from the lowest significant bit.
(1 | 000 1001)
3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001)
4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example).

Thus 137 becomes:
1000 0001 0000 1001
can anyone help me do this code using | & and shif to left operators

Thanks
Aug 1 '07 #1
8 1461
SammyB
807 Expert 512MB
I need to convert any integer value greate than 127 to binary value using this example:
Represent the value in binary
(e.g 137 => 1000 1001)
2) Break in up in groups of 7 bits from the lowest significant bit.
(1 | 000 1001)
3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001)
4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example).

Thus 137 becomes:
1000 0001 0000 1001
can anyone help me do this code using | & and shif to left operators

Thanks
What are you having trouble with? What language are you using? What environment? (Windows App or Web App) Are you trying to create a string or two bytes?
Aug 1 '07 #2
sara77
7
I am using c# and i need to convert any integer greater than 127 to the format i mentioned. it can be sotred in Int32 format after the conversion is done. can you help?
i have a code that doesn't work most of the time!! i want to use | & >>
rather than this long complicated useless code. here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if (bmpWidth < 128 && bmpHeight < 128)
  3. {
  4.     binaryWriter.Write((byte)bmpWidth);
  5.     binaryWriter.Write((byte)bmpHeight);
  6. }
  7. else
  8. {
  9.     String width = null;
  10.     String height = null;
  11.     int multibytes = 0;
  12.     String[] septenaries = null;
  13.     String currentSeptanary = null;
  14.     int septanaryCount = 0;
  15.     int bitCount = 0;
  16.     byte data = 0;
  17.  
  18.     if (bmpWidth > 127)
  19.     {
  20.         width = binaryConvertor(bmpWidth);
  21.         int widthLenght = width.Length;
  22.         multibytes = widthLenght / 7;
  23.         septenaries = new String[multibytes + 1];
  24.         currentSeptanary = null;
  25.         septanaryCount = 0;
  26.         bitCount = 0;
  27.         for (int count = 0; count < widthLenght; count++)
  28.         {
  29.             septanaryCount = count / 7;
  30.             bitCount++;
  31.             currentSeptanary = width[count] + currentSeptanary;
  32.             if (bitCount == 7)
  33.             {
  34.                 septenaries[septanaryCount] = currentSeptanary;
  35.                 bitCount = 0;
  36.                 currentSeptanary = null;
  37.             }
  38.         }
  39.         if (bitCount != 7)
  40.         {
  41.             septenaries[septanaryCount] = currentSeptanary;
  42.         }
  43.         for (int count = septenaries.Length - 1; count >= 0; count--)
  44.         {
  45.             if (count > 0) // all except the last width bytes should have 1 at bit location 8 to undicates that more length bytes are following
  46.             {
  47.                 if (septenaries[count].Length < 7)
  48.                 {
  49.                     //pad bits upfront until we have 7 characters 
  50.                     int initialLength = septenaries[count].Length;
  51.                     for (int count2 = 0; count2 < 7 - initialLength; count2++)
  52.                     {
  53.                         septenaries[count] = "0" + septenaries[count];
  54.                     }
  55.                 }
  56.                 //write a '1' at location 8
  57.                 septenaries[count] = "1" + septenaries[count];
  58.             }
  59.             else
  60.             {
  61.                 septenaries[count] = "0" + septenaries[count]; //last byte, bit location 8 = 0
  62.             }
  63.             data = (byte)Convert.ToInt64(septenaries[count], 2);
  64.             binaryWriter.Write(data);
  65.         }
  66.     }
  67. }
  68.  
Aug 1 '07 #3
SammyB
807 Expert 512MB
Ouch! Doesn't this do what you want?
Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             int n = int.Parse(textBox1.Text);
  4.             int lo7 = n & 0x7F;
  5.             int hi7 = n & 0x3F80;
  6.             hi7 <<= hi7;
  7.             if (hi7 != 0) hi7 += 0x8000;
  8.             int m = lo7 + hi7;
  9.             MessageBox.Show(n.ToString("X") + "-->" + m.ToString("X"));
  10.         }
  11.  
Aug 1 '07 #4
sara77
7
This is genius!!!! can you please explain the code a little bit?
for instance for 128 i should get 1000 0001 0000 0000
and for 137 i should get 1000 0001 0000 1001
with your code i get " 8080" for 128
how can i fix this?

Thank you sooooo much
Sarah
Aug 2 '07 #5
sara77
7
The maximum width/height one can have in a byte is 127.
A width of 128 will require two bytes: 1000 0001 0000 0000.
And a width of 137 will be: 1000 0001 0000 1001
The second bytes for the widths 128 and 137 have their first
bits as 0, so there are no more width bytes to follow."
Aug 2 '07 #6
sara77
7
Just to make mayself clear

"560 will be 0x84 0x30 when denoted as a variable length integer. (0x84 - 0x80)*128 + 0x30 = 512+ 48 = 560. "
so i should be able to get "8430" for 560
Aug 2 '07 #7
SammyB
807 Expert 512MB
This is genius!!!!
Well, I guess that it's not genius if it doesn't work! ;o)

The code should be:
Expand|Select|Wrap|Line Numbers
  1.      private void button1_Click(object sender, System.EventArgs e)
  2.      {
  3.          int n = int.Parse(textBox1.Text);
  4.          int lo7 = n & 0x7F;
  5.          int hi7 = n & 0x3F80;
  6.          hi7 = hi7 << 1;
  7.          if (hi7 != 0) hi7 += 0x8000;
  8.          int m = lo7 + hi7;
  9.          MessageBox.Show(n.ToString("X") + "-->" + m.ToString("X"));
  10.      }
  11.  
I created a windows app with a textbox and a button, hence line 1.
When the user enters a number in the textbox and presses the button, line 3 converts it into an int, n.
Line 4 grabs the low order 7 bits ( 7F hex = 111 1111 binary).
Line 5 grabs the high order 7 bits (3F80 hex = 11 1111 1000 0000 binary.
Line 6 shifts the high-order bits left one bit
Line 7 checks if the high-order is non-zero, if it is non-zero, it sets the MSB by adding in that bit (8000 hex = 1000 0000 0000 0000 binary) -- is this what you want to do?
Line 8 just adds the two pieces together.
I could have ored the bits together, but since they were masked, addition was OK and more understandable.
Line 9 shows the input and output in hex.

HTH --Sam
Aug 2 '07 #8
sara77
7
Thank you so much Sam, my code is finally working!! you are a true genius! A++++++++++++++ programmer
Aug 2 '07 #9

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

Similar topics

11
by: Idar | last post by:
Is there an effecient/fast way in python to convert binary data from file (24bit hex(int) big endian) to 32bit int (little endian)? Have seen struct.unpack, but I am unsure how and what Python has...
4
by: arvind | last post by:
hi, How do I display a number in binary or octagonal ? eg. int a= 195; //1100 0011 int b = 87; //0101 0111 int c = a&b;//0100 0011 => bitwise and printf("%0x", c); // Here I get the...
6
by: Gernot Frisch | last post by:
template <class flt> void write(FILE* pF, const flt& f) { // this will definitely _not_ write // an x-platfrom binary file fwrite(&f, sizeof(f), 1, pF); } int main() {
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
2
by: Diablo | last post by:
Hi, I am converting a byte array to string using System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the resulting text is converted back to binary, using the reverse function:...
9
by: gamehack | last post by:
Hi all, I've been wondering when I write a structure like: struct { int a; unsigned int b; float c; } mystruct;
1
TMS
by: TMS | last post by:
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics: #ifndef...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
15
by: David Marsh | last post by:
I accidentally typed %b instead of %d in a printf format string and got a binary representation of the number. Is that standard C or a compiler extension?
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
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
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...

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.