0

How to show hexadecimal is Stringformat by using .NET

บทความนี้เป็นการเขียนโปรแกรม เพื่อดึงค่าที่อยู่ในรูปแบบ 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

เรื่องที่เกี่ยวข้อง:

  1. How to XOR Encryption by using .Net
  2. How to MD5 encrypt and MD5 decrypt in C#
  3. How to string IndexOf using C#
  4. การเขียนโปรแกรมจัดการ Array อย่าง่าย ด้วย List Class
  5. การเขียนโปรแกรมเข้ารหัสด้วย Tripple-DES ด้วย C#.NET
Filed in: .NET Tags: , , , ,

Related Posts

Bookmark and Promote!

Leave a Reply

Submit Comment
*

© 7149 ไอที-เดฟโซน. All Rights Reserved. XHTML / CSS Valid.