当前位置:   article > 正文

C# STRING TO SHORT[]方法_c# string 转 short

c# string 转 short

C# STRING TO SHORT[]

版权声明:本文博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
根据https://blog.csdn.net/RazilFelix/article/details/52314542 修改 ,在原方法上修改后才能用

public short[] stringToShort(String inString, out short[] outShort)
    {
     if (inString.Length % 2 == 1) { inString += " "; }
        char[] bufChar = inString.ToCharArray();
        byte[] oufByte = Encoding.Default.GetBytes(bufChar);   // new byte[bufChar.Length];
        byte[] bufByte = new byte[2];
        outShort = new short[bufChar.Length / 2];
        for (int i = 0, j = 0; i < bufChar.Length; i += 2, j++)
        {
            bufByte[0] = oufByte[i];
            bufByte[1] = oufByte[i + 1];
            outShort[j] = BitConverter.ToInt16(bufByte, 0);
        }
        return outShort;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        short[] arr2 = new short[16];
        String inString = "12ABCDEFGHIJKLMNOPQRSTUVWXYZPP34";
        stringToShort(inString, out arr2);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号