字符串到HEX字节数组

时间:2015-07-17 10:10:01

标签: c# hex bytearray

我喜欢将String转换为HEX字节数组。

从类似的东西"例子" to byte [] exampleconv = {0x65,0x78,0x61,0x6d,0x70,0x6c,0x65}(来源:http://www.asciitohex.com/)。

我也在stackoverflow上搜索例子,但大多数示例将代码从字符串转换为十进制字节数组或类似。我没找到任何工作!将字符串转换为十六进制字节数组的示例(如上面的exampleHEX所示)。

2 个答案:

答案 0 :(得分:2)

使用byte[] ba = Encoding.Default.GetBytes("example"); // jsut to Display var hexString = BitConverter.ToString(ba); Console.WriteLine(hexString); 获取字节数组。示例代码:

#!/bin/bash

startDate=$(echo $2 | cut -d "-" -f 1 | cut -d "," -f 1)
endDate=$(echo $2 | cut -d "-" -f 2 | cut -d "," -f 1)
startTime=$(echo $2 | cut -d "-" -f 1 | cut -d "," -f 2)
endTime=$(echo $2 | cut -d "-" -f 2 | cut -d "," -f 2)

#Script Parameter Format to search in Log Files: DD.MM.YYYY,hh:mm-DD.MM.YYYY,hh:mm
timestampStart=$(echo $startDate | cut -d "." -f 3)-$(echo $startDate | cut -d "." -f 2)-$(echo $startDate | cut -d "." -f 1)" "$startTime
timestampEnd=$(echo $endDate | cut -d "." -f 3)-$(echo $endDate | cut -d "." -f 2)-$(echo $endDate | cut -d "." -f 1)" "$endTime

tStart=`date --date="$timestampStart" +%s`
tEnd=`date --date="$timestampEnd" +%s`

while read line; do
  re="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}"
  if [[ $line =~ $re ]]; then
    searchDate=$(echo $line | cut -d "," -f 1)
    tSearch=`date --date="$searchDate" +%s`
  fi

  if [ $tSearch -ge $tStart ] && [ $tSearch -lt $tEnd ];then
    echo $line
  fi
done < logs/backup/log/my_test.log

你会得到&#34; 65-78-61-6D-70-6C-65&#34;

答案 1 :(得分:1)

字节数组以二进制形式存储,无论它们如何呈现给消费者。

如果你考虑一下你读取数组的格式,而不是存储在数组中的数字类型,你会得到更多的运气。