2

I need help with convert numbers from byte array to string in C#.

There is my code:

string Astr = "123456789";
byte[] AByte = Astr.Select(c => (byte)(c - '0')).ToArray(); 
Astr = AByte.ToString(); // This is problem - I need convert numbers back to string

Thank you for yours ideas :)

user3299032
  • 53
  • 1
  • 5

3 Answers3

1

What about this,

Astr = new String(AByte.Select (b=>(Char)(b+ 48)).ToArray())
Tilak
  • 30,108
  • 19
  • 83
  • 131
0
var answer = string.Concat(AByte);

The overload used was new in .NET 4.0 (2010).

This works only if all values in the array are in the range (byte)0 through (byte)9. As pointed out in the comments, nine string instances will be created. However, no lambda (anonymous function) or intermediate array is used.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • You need to account for the `c - '0'` of the original code. Also, I think Concat will call tostring on each char, thus making 9 separate string instances before they are concatenated together. – driis Feb 11 '14 at 21:24
-1

A reasonably efficient approach would be to use the String constructor that takes a char array. Then you simply need to use Select to map your values into the original char values, and call ToArray().

string originalString = new String(AByte.Select(x => (char)(x + '0')).ToArray());

This question and answer suggests that using a StringBuilder in a loop could be marginally faster, however, I don't think it's worth it unless you know you have a bottleneck.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341
  • There is no String constructor taking IEnumerable – L.B Feb 11 '14 at 21:15
  • Argh, didn't they add that yet ? ;-) Fixed the answer. – driis Feb 11 '14 at 21:20
  • Downvoting. This creates an extra redundant array. – Jeppe Stig Nielsen Feb 11 '14 at 21:23
  • It's better than 9 redundant strings. Not downvoting your answer though. – driis Feb 11 '14 at 21:25
  • Hmm, you are right about the strings in my answer. However I avoid 9 additions, a lambda and an extra array. – Jeppe Stig Nielsen Feb 11 '14 at 21:29
  • But the code in your answer doesn't work, you need to add the 48 ( `0` ) back in to get the original string, unless the problem space is limited to digits :-) Also, http://stackoverflow.com/questions/14778840/is-the-string-ctor-the-fastest-way-to-convert-an-ienumerablechar-to-string - tl;dr: This is fast, throwing in a stringbuilder would be marginally faster. – driis Feb 11 '14 at 21:32
  • No, subtracting `'0'` (in the expression `c - '0'` of the question) is really just a "smart" way of doing `byte.Parse(c.ToString())`. – Jeppe Stig Nielsen Feb 11 '14 at 21:36
  • @driis `But the code in your answer doesn't work, you need to add the 48 ( 0 ) back in to get the original string` Have you tried before commenting? – L.B Feb 11 '14 at 21:37
  • @L.B ... `unless the problem space is limited to digits` – driis Feb 11 '14 at 21:38
  • @driis Then I should ask, have you read the question. How the OP forms that byte array. – L.B Feb 11 '14 at 21:39