Click here to Skip to main content
12,693,891 members (32,884 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
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 23-Jan-13 2:10am
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?
glennPattonWork 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Here is an example of a simple solution:

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;
  Permalink  
Comments
glennPattonWork 24-Jan-13 11:58am
   
Thanks alot, but I have solved this problem!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

There's no need to Split, this can be done character-by-character from the string:
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.
  Permalink  
Comments
glennPattonWork 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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

If you business rule is simply to reverse the order of every two chars of a string you could use this simple code:

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:
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]);
  Permalink  
v9
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100