บทความนี้เป็นการเขียนโปรแกรม เพื่อดึงค่าที่อยู่ในรูปแบบ Byte หรือ hexadecimal (hex) มาแสดงเป็นรูปแบบสตริง (String)
อย่างเช่น ตัวอย่างซอร์คโค้ดต่อไปนี้
|
1 2 3 4 5 6 |
byte[] array = new byte[5];
array[0] = 0x53;
array[1] = 0xA4;
array[2] = 0xB5;
array[3] = 0xC6;
array[4] = 0xFF; |
ต้องการให้แสดงผลใน TextBox ปกติ ก็จะแสดง FF ก็จะแสดง “255″ ,A4 ก็จะแสดง “164″ แต่สิ่งที่เราต้องการ ก็คือ ค่า FF ก็ต้องแสดงผลใน Textbox “FF” ด้วยเช่นกัน จะเขียนโค้ดอย่างไร ง่ายนิดเดียวครับ ไปดูกันเลย
ภาษาC#
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public string ShowHexString(byte[] str)
{
string txt = "";
foreach (byte b in str)
{
int t = Convert.ToInt32(b);
txt += t.ToString("X2");
}
return txt;
} |
ภาษา VB.NET
|
1 2 3 4 5 6 7 8 9 10 11 |
<pre class="brush:csharp">Private Function GetHEXstring(str As Byte()) As String
Dim txt As String = ""
For Each b As Byte In str
Dim t As Integer = Convert.ToInt32(b)
txt += t.ToString("X2")
Next
Return txt
End Function |
Incoming search terms:
- ให้ ค่าใน textbox Xor กัน
- c# hex
- แปลง hex เป็น c
- C# socket hex
- vb เขียนค่า hex
เรื่องที่เกี่ยวข้อง:



