| | 1 | | @using System.Text |
| | 2 | | @inject IJSRuntime JSRuntime |
| | 3 | |
|
| | 4 | | <div class="container"> |
| | 5 | | <div class="row"> |
| | 6 | | <div class="col"> |
| | 7 | | <textarea id="binary" class="form-control" rows="5" @bind="Input"></textarea> |
| | 8 | | </div> |
| | 9 | | </div> |
| | 10 | |
|
| | 11 | | <div class="row"> |
| | 12 | | <div class="col"> |
| | 13 | | <button id="btnBinary" name="btnBinary" class="btn btn-success float-right" @onclick="Swap">Swap</button> |
| | 14 | | <button id="btnBinaryClear" name="btnBinaryClear" class="btn btn-danger float-right" @onclick="Clear"><i cla |
| | 15 | | </div> |
| | 16 | | </div> |
| | 17 | | <div class="row"> |
| | 18 | | <div class="col"> |
| | 19 | | <textarea id="outputBinary" class="form-control" @bind="Output"></textarea> |
| | 20 | | </div> |
| | 21 | | </div> |
| | 22 | | <div class="row"> |
| | 23 | | <div class="col"> |
| | 24 | | <button id="btnCopyBinary" name="btnCopyBinary" class="btn btn-info float-right" @onclick="Copy"><i class="f |
| | 25 | | </div> |
| | 26 | | </div> |
| | 27 | |
|
| | 28 | | </div> |
| | 29 | |
|
| | 30 | | @code { |
| 0 | 31 | | string Input = "01101111 01101110 01100101 00100000 01101100 01101001 01101110 01100101"; //string.Empty; |
| 0 | 32 | | string Output = string.Empty; |
| | 33 | |
|
| | 34 | | void Swap() |
| 0 | 35 | | { |
| 0 | 36 | | Input = String.Concat(Input.Where(c => !Char.IsWhiteSpace(c))); |
| 0 | 37 | | var data = GetBytesFromBinaryString(Input); |
| 0 | 38 | | var text = Encoding.ASCII.GetString(data); |
| 0 | 39 | | Output = text; |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | private void Clear() |
| 0 | 43 | | { |
| 0 | 44 | | Input = string.Empty; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | async Task Copy() |
| 0 | 48 | | { |
| 0 | 49 | | await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", Output); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | // https://stackoverflow.com/a/6008872/2895831 |
| | 53 | | private Byte[] GetBytesFromBinaryString(String binary) |
| 0 | 54 | | { |
| 0 | 55 | | var list = new List<Byte>(); |
| | 56 | |
|
| 0 | 57 | | for (int i = 0; i < binary.Length; i += 8) |
| 0 | 58 | | { |
| 0 | 59 | | String t = binary.Substring(i, 8); |
| | 60 | |
|
| 0 | 61 | | list.Add(Convert.ToByte(t, 2)); |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | return list.ToArray(); |
| 0 | 65 | | } |
| | 66 | | } |