Home > Net >  How to turn a string of hexadecimal ASCII array form
How to turn a string of hexadecimal ASCII array form

Time:10-08



Such as string buf="124" + "" +" 245 "+" "+" 789 ";//"" said the blank space in the middle of the
//need to convert hexadecimal ASCII form, put into a byte array, good to send out a serial port communication,

Byte [] buf16=the string converted to hexadecimal array form;

CodePudding user response:


 
Byte [] buf16=Encoding. The ASCII. GetBytes (buf);

CodePudding user response:

 buf. Split (' '). The Select (s=& gt; The Convert. ToByte (s)). ToArray () 

CodePudding user response:

reference 1st floor github_36000833 response:
 
Byte [] buf16=Encoding. The ASCII. GetBytes (buf);


This is 10 hexadecimal conversion, I monitor

CodePudding user response:

references on the 2nd floor buses meet response:
 buf. Split (' '). The Select (s=& gt; The Convert. ToByte (s)). ToArray () 


Output array in where

CodePudding user response:

In line of time is not short ah, give you is not complete a
 byte [] buf16=buf. Split (' '). The Select (s=& gt; The Convert. ToByte (s)). ToArray (); 

CodePudding user response:

To add a 16, is the hexadecimal,
Code=csharp]
Byte [] buf16=buf. Split (' '). The Select (s=& gt; The Convert. ToByte (s, 16)). ToArray ();
[/code]

But the hex is more than 255, 124 byte can not let go, you may want to two to two, write a function,

 

Private byte [] StringToBytes16 (string source)
{
Byte [] destination=new byte [source. Length/2];
For (int I=0, j=0; I & lt; Source. The Length & amp; & J & lt; Source. Length/2; I +=2, j++)
{
String item=source. The Substring (I, 2);
Destination [j].=the Convert ToByte (item, 16);
}
The return destination;
}

  •  Tags:  
  • C#
  • Related