数组Byte [] 和 string 相互转换

using System;
using System.Collections.Generic;
using System.Text;

namespace NET.MST.Fourth.StringByte
{
    class StringByte
    {
        static void Main(string[] args)
        {
            String s = "我是字符串,I am string";

            //字节数组转换到字符串
            Byte[] utf8 = StringToByte(s,
                Encoding.UTF8);
            Byte[] gb2312 = StringToByte(s,
                Encoding.GetEncoding("GB2312"));
            Byte[] unicode = StringToByte(s,
                Encoding.Unicode);
            Console.WriteLine(utf8.Length);
            Console.WriteLine(gb2312.Length);
            Console.WriteLine(unicode.Length);

            //转换回字符串
            Console.WriteLine(ByteToString(utf8,
                Encoding.UTF8));
            Console.WriteLine(ByteToString(gb2312,
                Encoding.GetEncoding("GB2312")));
            Console.WriteLine(ByteToString(unicode,
                Encoding.Unicode));
            Console.Read();
        }
        static Byte[] StringToByte(String s, Encoding encoding)
        {
            return encoding.GetBytes(s);
        }
        static String ByteToString(Byte[] b, Encoding encoding)
        {
            return encoding.GetString(b);
        }
    }
}
时间: 2024-08-07 08:40:22

数组Byte [] 和 string 相互转换的相关文章

C#中字节数组(byte[])和字符串相互转换

转换过程主要使用到System.Text.Encoding命名空间下的类 1. 字符串转换成字节数组byte[]: string str = "This is test string"; byte[] byteArray = System.Text.Encoding.Default.GetBytes(str); 2.字节数组换成字符串: byte[] byteArray = 通过某种方式获取到的字节数组 string str = System.Text.Encoding.Default

golang []byte 和 string相互转换

原文链接:golang []byte和string相互转换 测试例子 package main import ( "fmt" ) func main() { str2 := "hello" data2 := []byte(str2) fmt.Println(data2) str2 = string(data2[:]) fmt.Println(str2) } 测试结果: [[email protected] ]# go run d.go [104 101 108 10

C#-----字节数组(byte[])和字符串相互转换

   Encoding类  表示字符编码 1.字符串转换成字节数组byte[] using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FileStreamTest { class Program { static void Main(string[] args) {

golang []byte和string相互转换

测试例子 package main   import (     "fmt" )   func main() {     str2 := "hello"     data2 := []byte(str2)     fmt.Println(data2)     str2 = string(data2[:])     fmt.Println(str2) }

Delphi Byte数组与Int String之间的相互转换

http://www.cnblogs.com/lcw/p/3352864.html string string = AnsiString = 长字符串,理论上长度不受限制,但其实受限于最大寻址范围2的32次方=4G字节: 变量Str名字是一个指针,指向位于堆内存的字符序列,字符序列起始于@Str[1],@Str[1]偏移负16个字节的空间存储着字串长度.引用计数等信息.字符序列以NULL结束. string[n] string[n] = ShortString = 短字符串,最多容纳255个字符

C# 跨线程调用form控件技巧及byte[]与string型相互转换

跨线程调用form控件技巧 private delegate void MethodSocket(object obj);//使用托管 ss = "OK"; this.BeginInvoke(new MethodSocket(InvokerReadMsg), ss);//this指向本窗体,回调函数InvokerReadMsg, private void InvokerReadMsg(object obj)//在这个函数里面可以直接访问Form控件<span style=&quo

C# 中字符串string和字节数组byte[]的转换

string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string: string str = System.Text.Encoding.Default.GetString ( byteArray ); string转ASCII byte[]: byte[] byteArray = System.Text.Encoding.ASCII.GetBytes ( str ); AS

【c++基础】字符数组和string相互转换

字符数组转化成string类型char ch [] = "ABCDEFG";string str(ch);//也可string str = ch;或者char ch [] = "ABCDEFG";string str;str = ch;//在原有基础上添加可以用str += ch; 将string类型转换为字符数组char buf[10];string str("ABCDEFG");length = str.copy(buf, 9);buf[le

XE6 ShortString与String相互转换

program Test; {$APPTYPE CONSOLE} uses System, System.SysUtils; const Value: array[0..5] of Byte = (5, 72, 101, 76, 76, 111); { Old ShortString representation of 'Hello' } type EShortStringConvertError = class(Exception) end; function ShortStringToStr