国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

16進制字符和圖片之間相互轉換

2019-11-14 15:54:55
字體:
來源:轉載
供稿:網友

  圖片和字符轉換一版用在socket進行通信之間。現在我就把我寫的和測試整理出來和大家分享下

1:圖片轉換成16進制字符

 1   FileStream fs = new FileStream(lbl_show.Text, FileMode.Open, Fileaccess.Read); 2             BinaryReader br = new BinaryReader(fs); 3             StreamWriter sw = new StreamWriter(tb_position.Text); 4             int length = (int)fs.Length; 5             StringBuilder sb = new StringBuilder(); 6             while (length > 0) 7             { 8                 byte tempByte = br.ReadByte(); 9                 string tempStr = Convert.ToString(tempByte, 16);10                 if (tempStr.Length <= 1)11                 {12                     tempStr = "0" + tempStr;13                 }14                 sb.Append(tempStr);15                 length--;16             }17             sw.Write(sb.ToString());18             fs.Close();19             br.Close();20             sw.Close();21             MessageBox.Show("轉換成功"); 

注釋1:lbl_show.Text表示圖片存在的位置

注釋2:tb_position.Text表示字符保存位置

注釋3:string tempStr = Convert.ToString(tempByte, 16); 字節轉換成16進制字符串

2:16進制字符轉換成圖片

 1     FileStream fs = new FileStream("Imgs//test.jpg", FileMode.Create, FileAccess.Write); 2             BinaryWriter bw = new BinaryWriter(fs); 3             StreamReader sr = new StreamReader(lbl_text.Text); 4             while (sr.Peek() != -1) 5             { 6                 string tempStr = sr.ReadToEnd(); 7                 if (tempStr.Contains("7D01") || tempStr.Contains("7D02")) 8                 { 9                     tempStr = tempStr.Replace("7D02", "7E");10                     tempStr = tempStr.Replace("7D01", "7D");11                 }12                 int tlenth = tempStr.Length / 2;13                 int pos = 0;14                 string[] str = new string[tlenth];15                 for (int i = 0; i < tlenth; i++)16                 {17                     str[i] = tempStr.Substring(pos, 2);18                     pos = pos + 2;19                     string cc = str[i];20                     byte tempByte = Convert.ToByte(str[i], 16);21                     bw.Write(tempByte);22                 }23             }24             fs.Close();25             bw.Close();26             sr.Close();27             this.pictureBox1.Image = Image.FromFile("Imgs//test.jpg");

注釋1:Imgs//test.jpg 表示轉換圖片保存位置

注釋2:lbl_text.Text表示要轉換字符的位置

注釋3: byte tempByte = Convert.ToByte(str[i], 16);16進制字符轉成字符

運行效果

源碼下載


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宾阳县| 淳安县| 淅川县| 龙川县| 石景山区| 西吉县| 启东市| 定日县| 长治县| 河间市| 东至县| 高淳县| 卢龙县| 盐山县| 黄陵县| 多伦县| 民乐县| 河源市| 萍乡市| 崇左市| 庆安县| 衡水市| 东兰县| 南和县| 南城县| 清河县| 铜鼓县| 太和县| 颍上县| 常山县| 陆川县| 遂宁市| 渭南市| 横山县| 无极县| 三穗县| 华容县| 齐齐哈尔市| 虞城县| 靖远县| 沈丘县|