More Group Sites
Education Books
School Rankings
Jobless Net
Better Home
Enviro++
更好教育论坛


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Share
Options
View
Go to last post Go to first unread
hong  
#1 Posted : Saturday, July 7, 2018 8:41:50 AM(UTC)
hong

Rank: Administration

Reputation:

Groups: AcademicCoachingSchool, admin, Administration, BookSeller, CatholicSchool, CoachingAdult, CoachingProfessional, CoachingSports, ExtraCurriculumCoaching, IndependentSchool, Moderator, MusicTeacher, PrivateSchool, PublicSchool, SelectiveSchool, tutor
Joined: 11/23/2008(UTC)
Posts: 519

C# convert byte array to hex string

In C# / LINQ, there are many ways toshow binary in hex string.

Code:
public static void PrintDataInHex(byte[] data)
{
  //choose the way and format you like  

  //method 1: BitConverter.ToString
  //output hex string eg 41-80-00-01-D4-30
  var str = BitConverter.ToString(data);  

  //method 2: LINQ Concat or Join
  //use "X2" for uppercase hex letters, "x2" for lowercase
  //output hex string eg 41800001D430
  var str = string.Concat(data.Select(b => b.ToString("X2")).ToArray());  

  //method 3: same as method 2
  var str = string.Concat(data.Select(b => b.ToString("X2")));  

  //method 4: StringBuilder Append
  //output hex string eg 41800001D430
  StringBuilder sb = new StringBuilder();
  foreach (byte b in data)
    sb.Append(b.ToString("X2"));
  var str = sb.ToString();  

  //method 5: Byte Array to List, iterate to concate using lambda
  //output hex string eg 41800001D430
  var str = string.Empty;
  data.ToList().ForEach(b => str += b.ToString("X2"));
}

Edited by user Saturday, July 7, 2018 8:48:50 AM(UTC)  | Reason: Not specified

Sponsor
Rss Feed  Atom Feed
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.