byte array conversion
-
From a windows application form I need to print a ticket from a special ticket printer. that ticket includes a 2D barcode which is a string of all the information about a customer. I need to encrypt it and get as a byte array so I use Cryptoservices and done that. to concatenate that data with other data I've done something like this System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); string str = System.Text.Encoding.ASCII.GetString(b); So printing part is working fine. But when I scan the barcode and try to get it back to byte array like this, byte[] b = System.Text.Encoding.ASCII.GetBytes(data); Then my array size gets smaller and when I try to decrypt it it throws an error saying that invalid length of string. can anybody help me.
-
From a windows application form I need to print a ticket from a special ticket printer. that ticket includes a 2D barcode which is a string of all the information about a customer. I need to encrypt it and get as a byte array so I use Cryptoservices and done that. to concatenate that data with other data I've done something like this System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); string str = System.Text.Encoding.ASCII.GetString(b); So printing part is working fine. But when I scan the barcode and try to get it back to byte array like this, byte[] b = System.Text.Encoding.ASCII.GetBytes(data); Then my array size gets smaller and when I try to decrypt it it throws an error saying that invalid length of string. can anybody help me.
You can't use ASCII encoding to get the bytes as a string. It only support seven bit character codes, which means that any byte that is between 128 and 255 is replaced by a question mark. Actually, you shouldn't use any text encoding at all to turn arbitrary bytes into text. Most encodings does't have a character for every possible byte value. Text encodings are used to turn text into bytes and back, not the other way around. You have to encode the bytes in a different way. You can for example use base64 encoding:
string str = Convert.ToBase64String(b); byte[] b = Convert.FromBase64String(str);
--- single minded; short sighted; long gone;
-
You can't use ASCII encoding to get the bytes as a string. It only support seven bit character codes, which means that any byte that is between 128 and 255 is replaced by a question mark. Actually, you shouldn't use any text encoding at all to turn arbitrary bytes into text. Most encodings does't have a character for every possible byte value. Text encodings are used to turn text into bytes and back, not the other way around. You have to encode the bytes in a different way. You can for example use base64 encoding:
string str = Convert.ToBase64String(b); byte[] b = Convert.FromBase64String(str);
--- single minded; short sighted; long gone;
-
when I use base64 its working fine. But i want to optimise the space taken by the barcode. I tried hex conversion and that also works fine. and then the size of the barcode is smaller. I want to make it much smaller.
Base64 puts 6 bits of data in each character, which only gives an overhead of 33%. That's the most compact way that you can easily represent binary data as text. Hexadeximal representation only puts 4 bits of data in each character, giving an overhead of 50%. Even if you find some complicated conversion that can squeeze almost 8 bits of data in each character, that is only about 30% better than base64. If you want to make the string substantially shorter, look at what the data represents and see if you can remove any unused parts of it.
--- single minded; short sighted; long gone;