字符串值转换为字节数组而不进行转换 [英] string values to byte array without converting

查看:81
本文介绍了字符串值转换为字节数组而不进行转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串的值放入一个字节数组,而不更改字符。这是因为字符串实际上是数据的字节表示。



目标是将输入字符串移动到字节数组中,然后使用以下方式转换字节数组:

 字符串结果= System.Text.Encoding.UTF8.GetString(data); 

我希望有人可以帮助我,尽管我知道这不是一个很好的描述。



编辑:
也许我应该解释一下,我正在处理的是带有文本框的简单Windows窗体,用户可以在其中将编码数据复制到其中,然后单击预览以查看解码后的数据。



编辑:
更多代码:
(inputText是文本框)

  private void button1_Click(对象发送者,EventArgs e)
{
string inputString = this.inputText.Text;
byte [] input =新的byte [inputString.Length];
for(int i = 0; i< inputString.Length; i ++)
{
input [i] = inputString [i];
}
字符串输出= base64Decode(input);
this.inputText.Text =;
this.inputText.Text =输出;
}

这是Windows表单的一部分,其中包含一个富文本框。该代码不起作用,因为它不会让我将char类型转换为字节。
但是如果我将行更改为:

  private void button1_Click(对象发送者,EventArgs e)
{
string inputString = this.inputText.Text;
byte [] input =新的byte [inputString.Length];
for(int i = 0; i< inputString.Length; i ++)
{
input [i] =(byte)inputString [i];
}
字符串输出= base64Decode(input);
this.inputText.Text =;
this.inputText.Text =输出;
}

它对值进行编码,但我不希望这样。我希望这可以更好地解释我的工作。



编辑:base64Decode函数:

 公共字符串base64Decode(byte []数据)
{
尝试
{
字符串结果= System.Text.Encoding.UTF8。 GetString(数据);
的返回结果;
}
catch(Exception e)
{
throw new Exception( base64Decode中的错误 + e.Message);
}
}

为清楚起见,未使用base64对该字符串进行编码。



请注意,这只是一行输入。



我知道了问题是我一直试图解码错误的格式。我感到非常愚蠢,因为当我发布示例输入时,我看到它必须是十六进制的,从那时起是如此简单。我使用此站点作为参考:
http://msdn.microsoft .com / zh-CN / library / bb311038.aspx



我的代码:

  public string [] getHexValues(string s)
{
int j = 0;
string [] hex = new String [s.Length / 2];
for(int i = 0; i< s.Length-2; i + = 2)
{
string temp = s.Substring(i,2);
this.inputText.Text = temp;
if(temp.Equals( 0x));;
else
{
hex [j] = temp;
j ++;
}
}
返回十六进制;
}

public string convertFromHex(string [] hex)
{
string result = null;
for(int i = 0; i< hex.Length; i ++)
{
int值= Convert.ToInt32(hex [i],16);
结果+ = Char.ConvertFromUtf32(value);
}
返回结果;
}

我现在感觉很愚蠢,但要感谢所有帮助的人,尤其是@Jon飞碟。

解决方案

您是说您有这样的东西吗?

  string s = 48656c6c6f2c20776f726c6421; 

,您是否希望这些值作为字节数组?然后:

  public IEnumerable< byte> GetBytesFromByteString(string s){
for(int index = 0; index< s.Length; index + = 2){
yield return Convert.ToByte(s.Substring(index,2),16 );
}
}

用法:

  string s = 48656c6c6f2c20776f726c6421; 
var bytes = GetBytesFromByteString(s).ToArray();

请注意,



<$ p $的输出p> Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(bytes));

 你好,世界! 

您显然需要使上述方法更加安全。


I'm trying to put the values of a string into a byte array with out changing the characters. This is because the string is in fact a byte representation of the data.

The goal is to move the input string into a byte array and then convert the byte array using:

string result = System.Text.Encoding.UTF8.GetString(data);

I hope someone can help me although I know it´s not a very good description.

EDIT: And maybe I should explain that what I´m working on is a simple windows form with a textbox where users can copy the encoded data into it and then click preview to see the decoded data.

EDIT: A little more code: (inputText is a textbox)

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = inputString[i];
        }
        string output = base64Decode(input);
        this.inputText.Text = "";
        this.inputText.Text = output;
    }

This is a part of a windows form and it includes a rich text box. This code doesn´t work because it won´t let me convert type char to byte. But if I change the line to :

    private void button1_Click(object sender, EventArgs e)
    {
        string inputString = this.inputText.Text;
        byte[] input = new byte[inputString.Length];
        for (int i = 0; i < inputString.Length; i++)
        {
            input[i] = (byte)inputString[i];
        }
        string output = base64Decode(input);
        this.inputText.Text = "";
        this.inputText.Text = output;
    }

It encodes the value and I don´t want that. I hope this explains a little bit better what I´m trying to do.

EDIT: The base64Decode function:

    public string base64Decode(byte[] data)
    {
        try
        {
            string result = System.Text.Encoding.UTF8.GetString(data);
            return result;
        }
        catch (Exception e)
        {
            throw new Exception("Error in base64Decode" + e.Message);
        }
    }

The string is not encoded using base64 just to be clear. This is just bad naming on my behalf.

Note this is just one line of input.

I've got it. The problem was I was always trying to decode the wrong format. I feel very stupid because when I posted the example input I saw this had to be hex and it was so from then on it was easy. I used this site for reference: http://msdn.microsoft.com/en-us/library/bb311038.aspx

My code:

     public string[] getHexValues(string s)
     {
        int j = 0;
        string[] hex = new String[s.Length/2];
        for (int i = 0; i < s.Length-2; i += 2)
        {
            string temp = s.Substring(i, 2);
            this.inputText.Text = temp;
            if (temp.Equals("0x")) ;
            else
            {
                hex[j] = temp;
                j++;
            }
        }
        return hex;
    }

    public string convertFromHex(string[] hex)
    {
        string result = null;
        for (int i = 0; i < hex.Length; i++)
        {
            int value = Convert.ToInt32(hex[i], 16);
            result += Char.ConvertFromUtf32(value);
        }
        return result;
    }

I feel quite dumb right now but thanks to everyone who helped, especially @Jon Skeet.

解决方案

Are you saying you have something like this:

string s = "48656c6c6f2c20776f726c6421";

and you want these values as a byte array? Then:

public IEnumerable<byte> GetBytesFromByteString(string s) {
    for (int index = 0; index < s.Length; index += 2) {
        yield return Convert.ToByte(s.Substring(index, 2), 16);
    }
}

Usage:

string s = "48656c6c6f2c20776f726c6421";
var bytes = GetBytesFromByteString(s).ToArray();

Note that the output of

Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetString(bytes));

is

Hello, world!

You obviously need to make the above method a lot safer.

这篇关于字符串值转换为字节数组而不进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆