0

My application gets data from a hardware device which is represented as a string, EG XYZ*012. I split the string up so I get “012” which I need to convert to an array of bytes.

The problem I have is that I want each digit to keep its value so the character “0” will be stored in a byte as 0 and character “1” will be stored in the byte as 1 etc. This is required because I need to work on the bits of the bytes. I’ve tried using the “GetBytes” command but it converts “0” into 48 which is not what I want.

Is there a command to do what I want or do I need to manually handle each character in the string separately in a loop?

2
  • 1
    Try just subtract 48 from each byte value... Commented Jul 29, 2011 at 9:33
  • 1
    @Rubens: Pheew. Always use/say: subtract '0' from each (char) value. Commented Jul 29, 2011 at 9:35

6 Answers 6

3

The following will normalize text character numbers, to their byte number equivalents:

byte[] bytes = data.Select(c => (byte)(c - '0')).ToArray();
Sign up to request clarification or add additional context in comments.

7 Comments

char is a type, you need @char. Better use another name. And who wants to know/remember that 48 == '0' ? Think of the poor reader.
@Henk Interesting point re. 48. It looks more obvious to me that normalization is going on.
ch - '0' tells a logical story, it is self-documenting. ch - 48 requires knowledge of ASCII codes. .
@Henk We are converting from ASCII, therefore, a knowledge of ASCII or a hint that is what's going on is useful. Replacing it with a meaningful constant would be helpful (e.g. AsciiAdjust), but we're allowed brevity fro code samples.
No, we are converting from some (preferably unknown) char encoding.
|
1

Yes, use a loop. You want a particualr conversion which is not standard:

string numString = "012";

var byteDigits = new byte[numString.Length];
for(int i = 0; i < byteDigits.Length; i++)
    byteDigits[i] = (byte)(numString[i] - '0')

Comments

1
string numString = "012";
var numChars = numString.ToCharArray();
var result = new byte[numChars.Length];

for (int i = 0; i < numChars.Length; i++)
{
   result[i] = System.Convert.ToByte(numChars[i]);
}

Comments

1
string s = "012";
byte[] bytes = s.Select(c => byte.Parse(c.ToString())).ToArray();

1 Comment

I don't get the -1, it's a working solution. Just needed a ')'.
0

using System.Linq you can do this. This will also skip over non digits.

var sourceString="012";
var result = sourceString.Where(c=>c>='0' && c<='9').Select(c=>(byte)(c-'0')).ToArray();

or if you want invalid characters to just be 255 you could do

var result = sourceString.Select(c=> (c>='0' && c<='9') ? (byte)(c-'0') : 255).ToArray();

Comments

0

I am not sure if i understand as what you want to achieve, Is this

List<byte> lstint = byval.Select(c => Convert.ToByte(c.ToString())).ToList();

For a Byte array

byte[] bytarr = byval.Select(c => Convert.ToByte(c.ToString())).ToArray();

3 Comments

Strings already present a char enumerator, no need for ToCharArray. OP was also after a byte[].
A List<byte> is not a byte[]
@Henk Ok .. changed for the byte[] version, thank chibacity .. i missed that part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.