byte[] cast to a string
-
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym! -
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym!Where are you getting the byte array from? If it involves multi-byte characters, shouldn't you be using a char[] array instead? :)
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym!Take a look at cultures and globalization. I assume you need an 8 bit encoder/decoder using some codepage. -- I am of The Clan of Xymox. I wear a pink kilt!
-
Where are you getting the byte array from? If it involves multi-byte characters, shouldn't you be using a char[] array instead? :)
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
Take a look at cultures and globalization. I assume you need an 8 bit encoder/decoder using some codepage. -- I am of The Clan of Xymox. I wear a pink kilt!
I'm not too familiar with the encoders/decoders or how unicode particularly works, but my concern here is that if i have for example: byte b1=0x85; byte b2=0x7f; the data i want to actually send should be: 0x857f which I believe would be a valid unicode char. first of all, i don't want and encoder to "pad" my bytes when building a string (0x0085007f), nor do I want a decoder interpretting the bytes and apparent chars... thereby messing up the raw bits. I tried forgetting about building the array as a string and just defining it as a byte[] in the struct, but I couldn't get the marshalling right (unable to marshal struct as unmanaged type; cannot determine size...) When using a string, I am not getting a runtime error, I am just not building the string properly because I am getting incorrect results. I am open for any and all suggestions! Thanks
-
I'm not too familiar with the encoders/decoders or how unicode particularly works, but my concern here is that if i have for example: byte b1=0x85; byte b2=0x7f; the data i want to actually send should be: 0x857f which I believe would be a valid unicode char. first of all, i don't want and encoder to "pad" my bytes when building a string (0x0085007f), nor do I want a decoder interpretting the bytes and apparent chars... thereby messing up the raw bits. I tried forgetting about building the array as a string and just defining it as a byte[] in the struct, but I couldn't get the marshalling right (unable to marshal struct as unmanaged type; cannot determine size...) When using a string, I am not getting a runtime error, I am just not building the string properly because I am getting incorrect results. I am open for any and all suggestions! Thanks
Well.. what is the byte array made of? Do you know what character set it is made of? The ASCII character set is 7-bit only, therefore anything above 0x7f is "invalid" and not representable by using that character set. Please see
Encoding.GetEncoding()
it should solve your problem. :) -- I am of The Clan of Xymox. I wear a pink kilt! -
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym!While it is counter-intuitive, what you want in this case is
UnicodeEncoding.GetString(byte[])
. Because the data inside a string is natively stored using that UnicodeEncoding, it is a null transform, leaving your bytes alone. (I suspect you'd better have an even number of bytes in your array if you want this to work. Herein lies a subtle inconsistency between BSTR's and System.Strings. BSTR's kind of admit to being used to pass random data around, and hence support odd numbers of bytes even though they are supposedly a strings of 16bit values. Your API hopefully won't care if you pad an extra null on the end if the length was odd.) -Blake -
Well.. what is the byte array made of? Do you know what character set it is made of? The ASCII character set is 7-bit only, therefore anything above 0x7f is "invalid" and not representable by using that character set. Please see
Encoding.GetEncoding()
it should solve your problem. :) -- I am of The Clan of Xymox. I wear a pink kilt! -
Then... uhm. why, if I may ask, on earth are you trying to cast an array of bytes into a string? :confused: -- I am of The Clan of Xymox. I wear a pink kilt!
-
While it is counter-intuitive, what you want in this case is
UnicodeEncoding.GetString(byte[])
. Because the data inside a string is natively stored using that UnicodeEncoding, it is a null transform, leaving your bytes alone. (I suspect you'd better have an even number of bytes in your array if you want this to work. Herein lies a subtle inconsistency between BSTR's and System.Strings. BSTR's kind of admit to being used to pass random data around, and hence support odd numbers of bytes even though they are supposedly a strings of 16bit values. Your API hopefully won't care if you pad an extra null on the end if the length was odd.) -Blakeyeah, it's always going to be a multiple of 4 bytes, so I don't need to worry about padding. I used the UnicodeEncoding and get a different, yet still incorrect, result. I wonder if I am not declaring the struct correctly or I don't know, I feel that I am so close, but am just missing something... Thanks for your help.
-
Then... uhm. why, if I may ask, on earth are you trying to cast an array of bytes into a string? :confused: -- I am of The Clan of Xymox. I wear a pink kilt!
Well, that is not necessarily my ultimate solution, but I have gotten runtime errors when trying to pass the data as arrays. I may be able to, but I can't quite get everything right. I chose a string at first because it is in a struct that mimics an unmanaged struct and the original, unmanaged struct defines it as a string. The whole struct is then passed by reference in an interop call to a windows API function. When I use a string, the code runs, but the data in the string is incorrect and I get incorrect results. When I have used byte[] or char[] or even StringBuilder, I got runtime errors, saying the struct cannot converted to unmanaged.... the size cannot be determined. I might be able to do it both ways, I just need to figure out what I'm missing...:((
-
Well, that is not necessarily my ultimate solution, but I have gotten runtime errors when trying to pass the data as arrays. I may be able to, but I can't quite get everything right. I chose a string at first because it is in a struct that mimics an unmanaged struct and the original, unmanaged struct defines it as a string. The whole struct is then passed by reference in an interop call to a windows API function. When I use a string, the code runs, but the data in the string is incorrect and I get incorrect results. When I have used byte[] or char[] or even StringBuilder, I got runtime errors, saying the struct cannot converted to unmanaged.... the size cannot be determined. I might be able to do it both ways, I just need to figure out what I'm missing...:((
Tym! wrote: Well, that is not necessarily my ultimate solution, but I have gotten runtime errors when trying to pass the data as arrays. I may be able to, but I can't quite get everything right. I'm still confident though that this is the right way to do it. God knows what the .NET runtime does to strings when you're not watching... :rolleyes: -- I am of The Clan of Xymox. I wear a pink kilt!
-
yeah, it's always going to be a multiple of 4 bytes, so I don't need to worry about padding. I used the UnicodeEncoding and get a different, yet still incorrect, result. I wonder if I am not declaring the struct correctly or I don't know, I feel that I am so close, but am just missing something... Thanks for your help.
What about something like this? I didn't test, so it might have a typo, but would this sort of approach work? I don't know how you want the ordering. -Jeff
private const string blankString = "";
public static string ConvertToString(byte[] bytes) {
if (bytes == null) {
return blankString;
}
int length = bytes.Length;
if (length == 0) {
return blankString;
}
else if ((length % 2) == 0) {
length /= 2;
char[] chars = new char[length];
for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
}
return new string(chars);
}
else {
length /= 2;
char[] chars = new char[length + 1];
for(int charIndex = 0, byteIndex = 0; charIndex < length; charIndex++, byteIndex += 2) {
chars[charIndex] = (char)((((int)bytes[byteIndex]) << 8) + ((int)bytes[byteIndex + 1]));
}
chars[length] = (char)(((int)bytes[bytes.Length - 1]) << 8);
return new string(chars);
}
} -
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym! -
I am trying to take a byte array and cast it to a string. The closest I have come is:
ASCIIEncoding.GetString Method (Byte[])
However, there is a restriction: "Any element of the bytes array that is greater than hexadecimal 0x7F is translated to a Unicode question mark ('?')." I need to keep that extra bit. Tym!