string-->byte array[]

解决方案 »

  1.   

    refer:http://www.cnblogs.com/anyanran/archive/2010/08/09/1795849.html
      

  2.   

    static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }static string GetString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }
      

  3.   

    static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }static string GetString(byte[] bytes)
    {
        char[] chars = new char[bytes.Length / sizeof(char)];
        System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
        return new string(chars);
    }
      

  4.   

    byte[] buff = Convert.FromBase64String(input);
      

  5.   


    希望像2楼这种误导别人的回帖少一点,真是“毁”人不倦。
    正确的做法是根据你字符串的编码得到一个Encoding对象,然后调用Encoding的GetBytes方法把字符串转为字节数组。
    如何获取Encoding对象,可以看看MSDN
    http://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.100).aspx
      

  6.   

    byte[] ByteArray = Encoding.Default.GetBytes("String");
      

  7.   


    希望像2楼这种误导别人的回帖少一点,真是“毁”人不倦。
    正确的做法是根据你字符串的编码得到一个Encoding对象,然后调用Encoding的GetBytes方法把字符串转为字节数组。
    如何获取Encoding对象,可以看看MSDN
    http://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.100).aspx
    我是在 stack 上看的方法 我觉得没错啊  http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp
      

  8.   


    希望像2楼这种误导别人的回帖少一点,真是“毁”人不倦。
    正确的做法是根据你字符串的编码得到一个Encoding对象,然后调用Encoding的GetBytes方法把字符串转为字节数组。
    如何获取Encoding对象,可以看看MSDN
    http://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.100).aspx
    我是在 stack 上看的方法 我觉得没错啊  http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp
    你根本就不看回复吗?What's ugly about this one is, that GetString and GetBytes need to executed on a system with the same endianness to work. So you can't use this to get bytes you want to turn into a string elsewhere. So I have a hard time to come up with a situations where I'd want to use this. – CodesInChaos May 13 '12 at 11:14 
    9     
     
    @CodeInChaos: Like I said, the whole point of this is if you want to use it on the same kind of system, with the same set of functions. If not, then you shouldn't use it. – Mehrdad May 13 '12 at 18:00 
    24     
     
    -1 I guarantee that someone (who doesn't understand bytes vs characters) is going to want to convert their string into a byte array, they will google it and read this answer, and they will do the wrong thing, because in almost all cases, the encoding IS relevant. – artbristol Jun 15 '12 at 11:07 
    59     
     
    @artbristol: If they can't be bothered to read the answer (or the other answers...), then I'm sorry, then there's no better way for me to communicate with them. I generally opt for answering the OP rather than trying to guess what others might do with my answer -- the OP has the right to know, and just because someone might abuse a knife doesn't mean we need to hide all knives in the world for ourselves. Though if you disagree that's fine too. – Mehrdad Jun 15 '12 at 14:04 
    10     
     
    This answer is wrong on so many levels but foremost because of it's decleration "you DON'T need to worry about encoding!". The 2 methods, GetBytes and GetString are superfluous in as much as they are merely re-implementations of what Encoding.Unicode.GetBytes() and Encoding.Unicode.GetString() already do. The statement "As long as your program (or other programs) don't try to interpret the bytes" is also fundamentally flawed as implicitly they mean the bytes should be interpreted as Unicode. – David Jul 11 '12 at 12:36 
      

  9.   


    希望像2楼这种误导别人的回帖少一点,真是“毁”人不倦。
    正确的做法是根据你字符串的编码得到一个Encoding对象,然后调用Encoding的GetBytes方法把字符串转为字节数组。
    如何获取Encoding对象,可以看看MSDN
    http://msdn.microsoft.com/zh-cn/library/system.text.encoding(v=vs.100).aspx
    我是在 stack 上看的方法 我觉得没错啊  http://stackoverflow.com/questions/472906/net-string-to-byte-array-c-sharp
    你根本就不看回复吗?What's ugly about this one is, that GetString and GetBytes need to executed on a system with the same endianness to work. So you can't use this to get bytes you want to turn into a string elsewhere. So I have a hard time to come up with a situations where I'd want to use this. – CodesInChaos May 13 '12 at 11:14 
    9     
     
    @CodeInChaos: Like I said, the whole point of this is if you want to use it on the same kind of system, with the same set of functions. If not, then you shouldn't use it. – Mehrdad May 13 '12 at 18:00 
    24     
     
    -1 I guarantee that someone (who doesn't understand bytes vs characters) is going to want to convert their string into a byte array, they will google it and read this answer, and they will do the wrong thing, because in almost all cases, the encoding IS relevant. – artbristol Jun 15 '12 at 11:07 
    59     
     
    @artbristol: If they can't be bothered to read the answer (or the other answers...), then I'm sorry, then there's no better way for me to communicate with them. I generally opt for answering the OP rather than trying to guess what others might do with my answer -- the OP has the right to know, and just because someone might abuse a knife doesn't mean we need to hide all knives in the world for ourselves. Though if you disagree that's fine too. – Mehrdad Jun 15 '12 at 14:04 
    10     
     
    This answer is wrong on so many levels but foremost because of it's decleration "you DON'T need to worry about encoding!". The 2 methods, GetBytes and GetString are superfluous in as much as they are merely re-implementations of what Encoding.Unicode.GetBytes() and Encoding.Unicode.GetString() already do. The statement "As long as your program (or other programs) don't try to interpret the bytes" is also fundamentally flawed as implicitly they mean the bytes should be interpreted as Unicode. – David Jul 11 '12 at 12:36 
    sorry 我的问题  抱歉。