将字符串转换为二进制并递减 [英] convert string to binary and overhand

查看:85
本文介绍了将字符串转换为二进制并递减的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串编码为二进制或十六进制代码,然后在另一个地方(在另一个窗口中)将其恢复为字符串.

I want to coding string to binary or hexidecimal code and in another place I want (in another window)comeback it to string .

推荐答案

您好,
试试这个-
Hi,
Try this-
string str = "Hello";
byte []arr = System.Text.Encoding.ASCII.GetBytes(str);




Or

string result = string.Empty;
foreach(char ch in yourString)
{
   result += Convert.ToString((int)ch,2);
}


输出-
这会将"Hello"翻译为10010001100101110110011011001101111

字符串转换为十六进制,反之亦然


OUTPUT-
this will translate "Hello" to 10010001100101110110011011001101111

String to Hex and vise versa

public string ConvertStringToHex(string asciiString)
{
    string hex = "";
    foreach (char c in asciiString)
    {
        int tmp = c;
        hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
    }
    return hex;
}

public string ConvertHexToString(string HexValue)
{
    string StrValue = "";
    while (HexValue.Length > 0)
    {
        StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
        HexValue = HexValue.Substring(2, HexValue.Length - 2);
    }
    return StrValue;
}


您可能需要base64编码.

You probaby need base64 encoding.

public string Encode(string str)
{
   byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
   return Convert.ToBase64String(encbuff);
}
public string Decode(string str)
{
   byte[] decbuff = Convert.FromBase64String(str);
   return System.Text.Encoding.UTF8.GetString(decbuff);
}


看看 BinaryWriter [ ^ ]和 BinaryReader [ MemoryStream [
Have a look at BinaryWriter[^] and BinaryReader[^]

They can be used with the MemoryStream[^] to facilitate reading and writing to binary formats.

Best regards
Espen Harlinn


这篇关于将字符串转换为二进制并递减的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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