C#我需要对字符串进行位置更改。 [英] C# I need to do a position change of a string.

查看:102
本文介绍了C#我需要对字符串进行位置更改。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友你好。我需要对字符串进行位置更改。这样:

texbo1.text将有一个这样的十六进制代码。

00 64 00 64我需要这个十六进制代码看起来像这样:

64 00 64 00.但这总是以几个十六进制的形式完成,与值无关。

所以我需要总是让这些字符串改变。

如果我使用此功能

  private   void  button3_Click( object  sender,EventArgs e)
{
string s = int .Parse(textBox1.Text).ToString( X4) ;
var regex = new 正则表达式( @ 。{2});
string r = regex.Replace(s, $& + );
textBox1.Text =(r + r);


}





我的尝试:



键入示例值100时,它返回我想要的值00 64 00 64,但位置错误。我需要的位置是64 00 64 00.

解决方案

& + );
textBox1.Text =(r + r);


}





我尝试过:



当键入示例值100时,它返回我想要的值00 64 00 64,但位置错误。我需要的位置是64 00 64 00。


我不完全确定除了100十进制之外,你的例子与100有什么关系是64 Hex - 但是为什么100会产生两个64值我不确定。



但是当你将它转换为十六进制字符串时,你看起来正在做的是反转一个数字的Endian。(PC存储值时是小端,但是int.ToString返回一个大端值)。

如果整数值包含0x01020304并且你想要一个十六进制字符串04 03 02 01那么最简单的方法是你se BitConverter将其更改为字节数组,并将其转换为字符串:

  int   value  = 0x01020304; 
byte [] bytes = BitConverter.GetBytes( value );
Console.WriteLine(BitConverter.ToString(bytes));
Array.Reverse(bytes); // 或者反过来......
Console.WriteLine(BitConverter.ToString(bytes) ));

会给你:

 04-03-02-01 
01-02-03-04

如果你想删除连字符,那也是微不足道的 - 你不需要正则表达式(使用大锤来破解坚果):

  int   value  = 0x01020304; 
byte [] bytes = BitConverter.GetBytes( value );
Console.WriteLine(BitConverter.ToString(bytes).Replace( - ,< span class =code-string> ));
Array.Reverse(bytes); // 或者反过来......
Console.WriteLine(BitConverter.ToString(bytes) ).Replace( - ));


谢谢大家的帮助。我是在朋友的帮助下得到的。





  public   static  字符串逆变器( string  H)
{
String ReturnValue;
int i;
ReturnValue = ;
for (i = H.Length / 2 ; i > = 1 ; i - = 1
{
if (i < H.Length / 2
{
ReturnValue = ReturnValue + ;
}
ReturnValue = ReturnValue + H [ 2 * i - 2 ] + H [ 2 * i - 1 ];
}
return ReturnValue;
}



private void button3_Click( object sender,EventArgs e)
{
string s = < span class =code-keyword> int .Parse(textBox1.Text).ToString( X4 );
var regex = new 正则表达式( @ 。{2});
string r = regex.Replace(s,

Hello friends. I need to do a position change of a string. In this way:
A texbo1.text will have a hex code like that.
00 64 00 64 I need this hex code to look like this:
64 00 64 00. But this will always be done in several hex independent of the values.
So I need to always have these strings change that way.
In case I use this function

private void button3_Click(object sender, EventArgs e)
       {
           string s = int.Parse(textBox1.Text).ToString("X4");
           var regex = new Regex(@".{2}");
           string r = regex.Replace(s, "$&" + " ");
           textBox1.Text = (r+r);


       }



What I have tried:

When typing an example value 100 it returns what I want 00 64 00 64, but in the wrong position. I need the position to be 64 00 64 00.

解决方案

&" + " "); textBox1.Text = (r+r); }



What I have tried:

When typing an example value 100 it returns what I want 00 64 00 64, but in the wrong position. I need the position to be 64 00 64 00.


I'm not exactly sure what your examples have to do with "100" other than 100 decimal is 64 Hex - but why 100 generates two "64" values I'm not sure.

However, what you appear to be doing is reversing the Endian-ness of a number when you convert it to a hex string. (PC's are little endian when they store values, but int.ToString returns a big endian value).
If the integer value contains 0x01020304 and you want a hex string "04 03 02 01" then the simplest way is to use BitConverter to change it to a byte array, and convert that to a string:

int value = 0x01020304;
byte[] bytes = BitConverter.GetBytes(value);
Console.WriteLine(BitConverter.ToString(bytes));
Array.Reverse(bytes);   //Or the other way ...
Console.WriteLine(BitConverter.ToString(bytes));

Will give you:

04-03-02-01
01-02-03-04

If you want to remove the hyphens, then that's trivial too - you don;t need a regex (which is using a sledgehammer to crack a nut):

int value = 0x01020304;
byte[] bytes = BitConverter.GetBytes(value);
Console.WriteLine(BitConverter.ToString(bytes).Replace("-",""));
Array.Reverse(bytes);   //Or the other way ...
Console.WriteLine(BitConverter.ToString(bytes).Replace("-", ""));


Thank you all for your help. I got it with the help of a friend.


public static String Inverter(string H)
 {
     String ReturnValue;
     int i;
     ReturnValue = "";
     for (i = H.Length / 2; i >= 1; i -= 1)
     {
         if (i < H.Length / 2)
         {
             ReturnValue = ReturnValue + " ";
         }
         ReturnValue = ReturnValue + H[2 * i - 2] + H[2 * i - 1];
     }
     return ReturnValue;
 }



 private void button3_Click(object sender, EventArgs e)
 {
     string s = int.Parse(textBox1.Text).ToString("X4");
     var regex = new Regex(@".{2}");
     string r = regex.Replace(s, "


这篇关于C#我需要对字符串进行位置更改。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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