Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi All,

Question is related to one yesterday but I have a string which contain numbers
such as 987654321 yesterday I asked how to reverse it and got a method (thank you!)
to 12345678 I have now discovered it should be displayed as 21436587 the 12 need to displayed as 21 (this is due to some funky firmware coding it is now my issue to get working!) so I have split up the string to be 1 2 3 4 5 6 7 8 using a space as a delimeter could I use a foreach loop to step through and a space as a delimeter along the lines of
C#
 StringBuilder Swap = new StringBuilder();  
string store;
string store_Prev;
string combine;
foreach(string value in Returned_Trim.Split(' '))
{
store_Prev = store;
value = store;
combine = store+store_Prev;
Swap.Append(combine);

}  

just hack together the above code in the code box probably lots of syntax errors but you can see (hopefully) what I mean.
Glenn
Posted
Comments
sjelen 24-Jan-13 8:42am    
Seems like big-endian/little-endian conversion problem.
Why don't you try to fix endianness while reading bytes from the device instead of messing around with strings?
glennPattonWork3 24-Jan-13 8:58am    
It is, but I'm not allowed to fix it! this botch is in a product that is out in the field and if I fixed it I'm likely to break software/hardware that is happy. It's the classic "we know this is wrong but make it work!" mutter,mutter

Here is an example of a simple solution:

C#
string strInput = "87654321";
string strOutput = "";
long intOutput = 0;
int x = 0;
// Ensure an even number of digits
if (strInput.Length % 2 != 0) {
    Interaction.MsgBox("Input string must be an even number of hexadecimal characters");
    System.Environment.Exit(0);
}
for (x = strInput.Length - 4; x >= 0; x += -2) {
    strOutput += strInput.Substring(x, 2);
}
strOutput = strInput.Substring(strInput.Length - 2, 2) + strOutput;
try {
    // Use Try block in case strInput did not contain Hexadecimal characters
    intOutput = Convert.ToInt64(strOutput, 16);
} catch (Exception ex) {
    Interaction.MsgBox(ex.Message);
    System.Environment.Exit(0);
}
Interaction.MsgBox(strOutput + "=" + intOutput.ToString());

.
.
This is the code from the example above that moves the characters around as you require:
for (x = strInput.Length - 4; x >= 0; x += -2) {
    strOutput += strInput.Substring(x, 2);
}
strOutput = strInput.Substring(strInput.Length - 2, 2) + strOutput;
 
Share this answer
 
Comments
glennPattonWork3 24-Jan-13 11:58am    
Thanks alot, but I have solved this problem!
There's no need to Split, this can be done character-by-character from the string:
C#
StringBuilder Swap = new StringBuilder();
int pos = 0;
foreach (char c in Returned_Trim)
{
  if ((pos & 1) == 1)
  {
    Swap.Insert(pos-1, c);
  }
  else
  {
    Swap.Append(c);
  }
  ++pos;
}

Remember that StringBuilder can deal with more types than just string when building.
 
Share this answer
 
Comments
glennPattonWork3 24-Jan-13 7:48am    
I have got the question to work, but it is the wrong question, I have asked a new version today!
If you business rule is simply to reverse the order of every two chars of a string you could use this simple code:

C#
string input = ""123456789;
string output = string.Empty;
for (int i = 0; i < input.Length - 1; i += 2)
     output = string.Format("{0}{1}{2}", output, input[i + 1], input[i]);


If your string is really long I would use StringBuilder like this:
C#
string input = "hdfshfjvsnghvdf4fh12gy5hh2g2h4rtf5htf5bh1xtfh45tfh5f5hf445th46tfh4646t486t3f1htyh84f";
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.Length - 1; i += 2)
     output.AppendFormat("{0}{1}", input[i + 1], input[i]);
 
Share this answer
 
v9
If that value is fix
then you can use below algorithm
1) Replace 12 from this string to 21
2) Replace 34 from this string to 43
3) Replace 56 from this string to 65
4) Replace 78 from this string to 87

Hope this helps if yes then accept answer and vote it
--RD
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900