Gulivert
Gulivert

Reputation: 17

How to Convert int to binary, then split it, then Convert it to int?

I have to communicate a frequency by USART to an ATmega88 et for doing this I send 4 Bytes. The 2 and 3 bytes are for the frequency and work like that :

117 and 48 => 30000 Hz

which look like :

01110101 and 00110000 => 0b0111010100110000

I can send 117 and 48 manually and it work.

But now I want to use a slider in my WPF application and use his value.

I have tried a lot of things and the more I can do is this :

    private void Freq_Click(object sender, RoutedEventArgs e)
    {

        Int64 value_freq = (Int64)frequence_slider.Value;

        string freq1 = Convert.ToString(value_freq, 2); //Convert to binary in a string
        Int64 result = Convert.ToInt64(freq1); //Convert string in a Int64
        Int64 result2 = result << 8; // does not work like I expect .. it return a base10 number instead of a binary ..
        Int32 result3 = Convert.ToInt32(result, 10);

        System.Windows.MessageBox.Show("Partie 1 : " + freq1.ToString() + "\n \n" + "Partie 2 : " + result.ToString() + "\n \n" + "Partie 3 : " + result2.ToString() + "\n \n" + "Partie 4 : " + result3.ToString());
    }

Hope someone understand what I am talking about.

For more explanation here his the code of the ATmega for this part from AtmelStudio :

        freq=(uart_getc()<<8)+(uart_getc());

Upvotes: 0

Views: 1181

Answers (2)

Paul Kertscher
Paul Kertscher

Reputation: 9713

I think there is some confusion about data types and representations in this. Int64.ToString() will always output an base10 number, for the integer does not hold any information about the representation.

The code

string freq1 = Convert.ToString(value_freq, 2);
Int64 result = Convert.ToInt64(freq1);

will convert freq1 to a string holding its binary representation. However, when you convert it to an Int64, you will get some sort of base10 number, which looks like a base2 number, because the method expects a base10 representation (I'm not sure about the last part, but the documentation does not state anything else).

To clarify, let me give you an example. Lets assume we'd has the number 2(10), which would be 10(2) (in the following I will denote the base in braces after the number). Now if you pass "10" to ToInt64, it'll not see 10(2), but 10(10), which would be 1010(2). You expected result << 8 to produce 1000000000(2), but actually it is 101000000000(2), because you shifted the 1010(2) not the 10(2). I hope this is kind of clear.

Now that we discussed the logical failure in the sample code, lets see, what we can do about it. Actually it is way easier than the code in the sample. Actually, you did provide 2 bytes, which will be sent, together representing a short int, thus I will work with that and hopefully you then may generalize to whatever applies to your roblem

short frequency = 30000;
byte high, low;
low = (byte)(frequency & 0x00FF); //cut the high byte of the short and write the remaining to low
high = (byte)(frequency >> 8);

Now high contains 117 and low contains 48.

Upvotes: 1

omriman12
omriman12

Reputation: 1690

int to binnary:

Integer.toString(value,2)

Split:

String.substring(int beginIndex, int endIndex)

binnary to int:

Integer.parseInt(value, 2);

Upvotes: 0

Related Questions