質問編集履歴
3
情報追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -91,6 +91,7 @@
|
|
91
91
|
|
92
92
|
|
93
93
|
var tableIndex = Convert.ToInt32(bit6, 2);
|
94
|
+
//ここでインデックスが配列の境界外ですとエラーが出ます
|
94
95
|
sb.Append(conversionTable[tableIndex]);
|
95
96
|
}
|
96
97
|
}
|
2
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,8 +17,29 @@
|
|
17
17
|
|
18
18
|
```ここに言語を入力
|
19
19
|
|
20
|
+
using System;
|
21
|
+
using System.Collections.Generic;
|
22
|
+
using System.Linq;
|
23
|
+
using System.Text;
|
20
24
|
|
25
|
+
namespace Base64
|
26
|
+
{
|
27
|
+
class Program
|
28
|
+
{
|
29
|
+
public static char[] conversionTable = new char[]
|
30
|
+
{
|
31
|
+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
32
|
+
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
33
|
+
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
34
|
+
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
35
|
+
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
36
|
+
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
|
37
|
+
'8', '9', '+', '/',
|
38
|
+
};
|
21
39
|
|
40
|
+
|
41
|
+
|
42
|
+
|
22
43
|
static void Main(string[] args)
|
23
44
|
{
|
24
45
|
Console.WriteLine("文字を入力してください");
|
@@ -68,8 +89,8 @@
|
|
68
89
|
var padCount = 6 - bit6.Length;
|
69
90
|
for (int j = 0; j < padCount; j++) bit6 += "0";
|
70
91
|
|
92
|
+
|
71
|
-
|
93
|
+
var tableIndex = Convert.ToInt32(bit6, 2);
|
72
|
-
//ここでインデックスが配列の境界外ですとエラー
|
73
94
|
sb.Append(conversionTable[tableIndex]);
|
74
95
|
}
|
75
96
|
}
|
@@ -82,7 +103,8 @@
|
|
82
103
|
|
83
104
|
}
|
84
105
|
|
85
|
-
|
106
|
+
|
107
|
+
|
86
108
|
}
|
87
109
|
}
|
88
110
|
```
|
1
追加編集為
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,4 +11,78 @@
|
|
11
11
|
char[] bits = bytes.SelectMany(octet => Convert.ToString(octet, 2).PadLeft(8, '0')) .ToArray();
|
12
12
|
```
|
13
13
|
char[] bits変数の中身でLINQのselectmanyやラムダを使わない方法を知りたいです。
|
14
|
-
宜しくお願い致します。
|
14
|
+
宜しくお願い致します。
|
15
|
+
|
16
|
+
追記___
|
17
|
+
|
18
|
+
```ここに言語を入力
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
static void Main(string[] args)
|
23
|
+
{
|
24
|
+
Console.WriteLine("文字を入力してください");
|
25
|
+
string text = Console.ReadLine();
|
26
|
+
Encoding encoding = new UTF8Encoding();
|
27
|
+
string aaa = Encode(text, encoding);
|
28
|
+
Console.WriteLine($"{aaa}");
|
29
|
+
Console.ReadKey();
|
30
|
+
|
31
|
+
}
|
32
|
+
|
33
|
+
public static string Encode(string text, Encoding encoding)
|
34
|
+
{
|
35
|
+
//stringビルダー型の変数を初期化
|
36
|
+
StringBuilder sb = new StringBuilder();
|
37
|
+
|
38
|
+
// 変更したい文字列を2進数に変換する
|
39
|
+
byte[] bytes = encoding.GetBytes(text);
|
40
|
+
|
41
|
+
//LINQを使わず文字列に展開
|
42
|
+
var list = new List<string>();
|
43
|
+
foreach (var octet in bytes)
|
44
|
+
{
|
45
|
+
list.Add(Convert.ToString(octet, 2).PadLeft(8, '0'));
|
46
|
+
}
|
47
|
+
|
48
|
+
var bits = list.ToArray();
|
49
|
+
var bit6 = "";
|
50
|
+
for (int i = 0; i < bits.Length; i++)
|
51
|
+
{
|
52
|
+
|
53
|
+
bit6 += bits[i];
|
54
|
+
|
55
|
+
//6ビット文字列を対応表で変換
|
56
|
+
if (bit6.Length == 6)
|
57
|
+
{
|
58
|
+
|
59
|
+
int tableIndex = Convert.ToInt32(bit6, 2);
|
60
|
+
sb.Append(conversionTable[tableIndex]);
|
61
|
+
|
62
|
+
bit6 = string.Empty;
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
if (i == bits.Length - 1 && bit6.Length != 0)
|
67
|
+
{
|
68
|
+
var padCount = 6 - bit6.Length;
|
69
|
+
for (int j = 0; j < padCount; j++) bit6 += "0";
|
70
|
+
|
71
|
+
var tableIndex = Convert.ToInt32(bit6, 2);
|
72
|
+
//ここでインデックスが配列の境界外ですとエラー
|
73
|
+
sb.Append(conversionTable[tableIndex]);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
while (sb.Length % 4 != 0) sb.Append("=");
|
79
|
+
|
80
|
+
return sb.ToString();
|
81
|
+
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
86
|
+
}
|
87
|
+
}
|
88
|
+
```
|