2

Did Binary and ASCII data type had been defined in C# 2.0?

I plan to detect several variables are Binary or ASCII or Integer(sbyte, byte, short, ushort, long, ulong) types.

I can use typeof(sbyte) ect.

But I failed to implement as this typeof(binary) or typeof(ascii). What I need is something like this typeof function to detect the variables are Binary or ASCII type?

[update]

           format code    Octal
Binary         001000      10
ASCII          010000      20
Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • There is no Binary Or Ascii type in .NET. What type your variables are going to be? – Andrew Bezzub Jan 24 '11 at 07:47
  • 2
    How about explaining your problem at a higher level? A good link: http://blogs.msdn.com/b/oldnewthing/archive/2006/03/23/558887.aspx – Ed S. Jan 24 '11 at 07:47
  • My variable is an string currently. I need validate its type bye several select case statement. – Nano HE Jan 24 '11 at 07:48
  • 1
    Are you trying to convert unknown data into a fitting datatype or are you having problem represent binary and/or ascii? – stefan Jan 24 '11 at 07:48
  • @stefan, Yes, I need try to convert unkonw data into a fitting datatype. I updated my original post. – Nano HE Jan 24 '11 at 07:51
  • Hmm... question http://stackoverflow.com/questions/4779695 sounds very similar, but posted by a different user. It seems unlikely to be a coincidence. What's going on here? – Jon Skeet Jan 24 '11 at 08:09

3 Answers3

3

Normally you would store text data in a string, and binary data in a byte[]. Do not try to store binary data in a string by applying an arbitrary encoding. For example, if you try to use Encoding.UTF8.GetString(binaryData) then it's highly likely that you won't be able to get the original data back again.

If you need to store binary data in a string, you should use something like Convert.ToBase64String (and then Convert.FromBase64String) which stores 3 bytes of binary data in each 4 characters in the string.

If you need to store data in a string and the type of the original data, you should either store the type separately, or keep a compound form such as "type:data". Sample strings might then be:

int:10
string:10 // The digits 1 and 0, but originally as text, not a number
binary:hFed234= // Base-64-encoded data
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Actually my variable already transferred to my validateFunction(string myVariable). (as this form). Then I must trypase the unknown variable and find it's data type. – Nano HE Jan 24 '11 at 08:04
  • @Nano HE: Then you have no hope of accurately recovering the original type - "10" can represent the number ten, or just two digits... and if binary data *is* passed to you, do you know how it's encoded into text? – Jon Skeet Jan 24 '11 at 08:07
  • Thanks for your comment and reply. I don't got idea of the encoded rule. That means,And I will the ValidateFun(EnumTypes aType, string myVariable) can be design as this form. I need to validate `myVariable` is `aType` (`EnumTypes.ASCII`. Or `EnumTypes.Binary`). Then return true or false. – Nano HE Jan 24 '11 at 08:22
  • @Nano HE: If you don't now how the data is encoded, there's absolutely no way of retrieving it. The design basically seems a mess - I would do everything you possibly can to change it. – Jon Skeet Jan 24 '11 at 09:14
2

Have a look at System.Text.Encoding and System.Text.Decoder.

CesarGon
  • 15,099
  • 6
  • 57
  • 85
1

You need to attempt-parse it into a fitting datatype. Set them up with priority.

Something like:

1) Try to parse integer, if fail continue
2) Try to parse text, if fail continue
3) Save binary

To autodetect encoding see Determine a string's encoding in C#

Community
  • 1
  • 1
stefan
  • 2,886
  • 21
  • 27