相关文章推荐
踏实的胡萝卜  ·  Sql ...·  2 周前    · 
玩命的小虾米  ·  关于“TypeError: ...·  1 周前    · 
发财的充值卡  ·  Array.prototype.find() ...·  1 月前    · 
兴奋的开水瓶  ·  JavaScript ...·  1 年前    · 
有胆有识的大蒜  ·  Jest Cannot use ...·  1 年前    · 
阳刚的蚂蚁  ·  用户对问题“SQL ...·  1 年前    · 
string input = 019F314A
I want byte[] bytes to equal { 0x01, 0x9F, 0x31, 0x4A }
.Because i want to make a frame format which required data like that.
Thanks in advance.
What I have tried:
public static byte[] StringToByteArray( String hex) int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for ( int i = 0 ; i < NumberChars; i += 2 ) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2 ), 16 ); return bytes; 0x is just a notation to represent the number as Hex
your function will return the output array as {1,159,49,74} which is an integer array and as well as equivalent to the each byte notation
01 -> 1 9F -> 159 31 -> 49 4A -> 74
suppose if you want the value to be in Hex notation then you will have to convert the value to string as
string[] HexString = result.ToList().Select(k => " 0x" + k.ToString( " x" )).ToArray(); // { "0x1","0x9F", "0x31", "0x4A" }
which wil be of no use, once again you will have to convert it to byte for the usage.
refer this Convert Between Hexadecimal Strings and Numeric Types [ ^ ]
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •