0

is there a way to check if the bytes in a byte[] is a valid string, so if it contains ASCII characters only.

if (isValidASCII(myByteArray)) {
....
}

something that I could use like the above example but with functionality.

4
  • 3
    How do you define a valid string? What is the encoding should be? What have you tried? Commented Jul 5, 2016 at 8:56
  • you're missing one closing braked in your provided code Commented Jul 5, 2016 at 8:59
  • All Byte arrays contain valid strings because all strings are Byte arrays, however valid doesn't mean meaningful and to answer that we need to know what you class as meaningful Commented Jul 5, 2016 at 9:09
  • 1
    You're likely not trying to do anything useful. If I had to venture a guess, you're looking at a Byte[] representing text in a non-ascii encoding, likely UTF-8 if you expect some "valid" characters. To do something useful, you need to figure out how the text is encoded, and use the proper decoder to get the string back out. The path you're taking now will only lead to misery. Commented Jul 5, 2016 at 9:18

4 Answers 4

6

Well, if

contains ASCII characters only

means symbols with codes within [32..127] (corresponding characters are [' '..'~']) - standard ASCII table characters with command ones excluded:

Boolean isAscii = myByteArray.All(b => b >= 32 && b <= 127)

However, a valid string being defined like that can well apper to be

 "$d|1 ?;)y" // this is a valid ASCII characters based string

or alike. If you want to wrap this simple Linq into a method:

  public static bool isValidASCII(IEnumerable<byte> source) {
    if (null == source)
      return true; // or false, or throw exception

    return source.All(b => b >= 32 && b <= 127);
  }

  ...

  if (isValidASCII(myByteArray)) {
     ...
  }
Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't get the result until I included the low bytes: carriage return, tab, etc. Thus, to simplify, this will be quite enough: return source.All(b => b <= 127);
1

Literally answering your question:

Yes. It's not only very easy, it's trivial:

Boolean isValidAscii(Byte[] bytes) {
  return true;
}

This is most likely not what you're looking for.

ASCII is a table that maps each byte to a character. By definition every byte represents a valid ASCII character.

So the question really is, what are you looking for? What is a valid ASCII character in your opinion.

Once you define that, it's easy to code:

Boolean iFindThisAValidAsciiCharacter(Char c) {
  //your logic here
}

Boolean isValidAscii(Byte[] bytes) {
  return bytes.forAll((Byte b) => iFindThisAValidAscciCharacter( (char)b))
}

The trick, of course, is in your definition of what you consider valid.

I advice you to take a step back, and consider why you want "valid ascii" in the first place. In this brave new world of internationalization and unicode, it sounds very unlikely that what you are trying to do is going to accomplish what you want to accomplish.

Comments

-1

First you can convert your bytes array into string using this

System.Text.Encoding.ASCII.GetString(BytesArray);

then check if it is valid or not.

1 Comment

this function will just replace bytes with value greater than 127, with "?"
-3
string asciiString = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(BYTEVARIABLE));

now check if the the string has some chars that have been changed to '?' if yes it wasn't ASCII only.

Comments

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.