无法从二进制转换为字符串

编程入门 行业动态 更新时间:2024-10-28 21:17:18
本文介绍了无法从二进制转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Hello codeproject成员.. 我尝试学习C#课程来创建一个可以从字符串转换为二进制的应用程序,反之亦然。 i使用此代码成功将字符串转换为二进制:

Hello codeproject member.. i am tried learning C# course to create an application that could to convert from string to binary and vice versa. i am successfull to convert from string to binary using this code :

public string StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2).PadLeft(8, '0')); } string result = sb.ToString(); textBox2.Text = result; return sb.ToString(); }

从字符串转换为二进制的用法

usage to convert from string to binary

StringToBinary(textBox1.Text);

但我的问题是如何再次从二进制转换为字符串? 当我尝试使用此代码时,我无法获得原始字符串。为什么?

but my problem is how to convert from binary to string again? when i am tried using this code i was fail to get the original string. why?

public string BinaryToString(string data) { List<Byte> byteList = new List<Byte>(); for (int i = 0; i < data.Length; i += 8) { byteList.Add(Convert.ToByte(data.Substring(i, 8), 2)); } string result = byteList.ToString(); textBox1.Text = result; return Encoding.ASCII.GetString(byteList.ToArray()); }

用法

usage

BinaryToString(textBox2.Text);

请教我,先生。提前谢谢。

please teach me, sir. thanks in advance.

推荐答案

没有二进制这样的数据类型,所以这个问题很简单。由于我们不知道你的目的是什么,你无法得到确定的答案。 但是,你可以用特定的编码表示用于Unicode二进制表示的任何字符串。编码必须是UTF之一,因为其他编码会导致数据丢失,因为它不能代表所有Unicode代码点。 例如,对于UTF -8(用于在文本文件和Internet上表示Unicode文本的最流行的编码),它看起来像这样: There is no such data type as "binary", so the question is simply ambiguous. As we don't know what is your purpose, you cannot get on definitive answer. However, you can represent any string in certain encoding used for binary representation of Unicode. The encoding has to be one of the UTFs, as other encodings will cause the loss of data, as the cannot represent all Unicode code points. For example, for UTF-8 (most popular encoding used to represent Unicode text in text files and on Internet), it would look like this: string myString = //... //... System.Text.Encoding encoding = System.Text.Encoding.UTF8; // change it to any other UTF in you want byte[] data = encoding.GetBytes(); // got UTF-8 representation string text = new string(encoding.GetChars(data)); // back to string // at this point, myString should be the same as text

-SA

这是我执行并获得正确输出的代码... private void button1_Click(object sender,EventArgs e) { StringToBinary(textBox1.Text); } private void button2_Click(object sender,EventArgs e) { textBox3.Text = BinaryToString(textBox2.Text); } public void StringToBinary(字符串数据) { StringBuilder sb = new StringBuilder(); foreach(char c in data.ToCharArray()) { sb.Append(Conve rt.ToString(c,2).PadLeft(8,'0')); } string result = sb.ToString(); textBox2.Text = result; //返回sb.ToString(); } 公共字符串BinaryToString(字符串数据) { List< byte> byteList = new List< byte>(); for(int i = 0; i< data.Length; i + = 8) { byteList.Add(Convert.ToByte(data.Substring(i,8),2)); } string result = byteList.ToString(); 返回Encoding.ASCII.GetString(byteList.ToArray()); } This is your code which i Executed and Got proper output... private void button1_Click(object sender, EventArgs e) { StringToBinary(textBox1.Text); } private void button2_Click(object sender, EventArgs e) { textBox3.Text = BinaryToString(textBox2.Text); } public void StringToBinary(string data) { StringBuilder sb = new StringBuilder(); foreach (char c in data.ToCharArray()) { sb.Append(Convert.ToString(c, 2).PadLeft(8, '0')); } string result = sb.ToString(); textBox2.Text = result; //return sb.ToString(); } public string BinaryToString(string data) { List<byte> byteList = new List<byte>(); for (int i = 0; i < data.Length; i += 8) { byteList.Add(Convert.ToByte(data.Substring(i, 8), 2)); } string result = byteList.ToString(); return Encoding.ASCII.GetString(byteList.ToArray()); }

HI ... 希望这个链接可以帮助你.. stackoverflow/questions/6006425/binary-to-corresponding-ascii-string-conversion [ ^ ] b $ b 快乐编码..... HI... Hope this link helps You.. stackoverflow/questions/6006425/binary-to-corresponding-ascii-string-conversion[^] Happy Coding .....

更多推荐

无法从二进制转换为字符串

本文发布于:2023-06-05 01:20:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!