Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. byte[] cast to a string

byte[] cast to a string

Scheduled Pinned Locked Moved C#
questiondata-structures
15 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Tym

    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

    J Offline
    J Offline
    Jorgen Sigvardsson
    wrote on last edited by
    #6

    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!

    T 1 Reply Last reply
    0
    • T 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!

      B Offline
      B Offline
      Blake Coverett
      wrote on last edited by
      #7

      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

      T 1 Reply Last reply
      0
      • J Jorgen Sigvardsson

        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!

        T Offline
        T Offline
        Tym
        wrote on last edited by
        #8

        The byte array is made of bytes. They do not represent characters.

        J 1 Reply Last reply
        0
        • T Tym

          The byte array is made of bytes. They do not represent characters.

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #9

          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!

          T 1 Reply Last reply
          0
          • B Blake Coverett

            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

            T Offline
            T Offline
            Tym
            wrote on last edited by
            #10

            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.

            J 1 Reply Last reply
            0
            • J Jorgen Sigvardsson

              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!

              T Offline
              T Offline
              Tym
              wrote on last edited by
              #11

              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...:((

              J 1 Reply Last reply
              0
              • T Tym

                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...:((

                J Offline
                J Offline
                Jorgen Sigvardsson
                wrote on last edited by
                #12

                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!

                1 Reply Last reply
                0
                • T Tym

                  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.

                  J Offline
                  J Offline
                  Jeff Varszegi
                  wrote on last edited by
                  #13

                  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);
                  }
                  }

                  1 Reply Last reply
                  0
                  • T 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!

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #14

                    Like Jorgen said u need to get your codepage. Get it with Encoding.Default. that should fix the issue. leppie::AllocCPArticle("Zee blog");
                    Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.

                    1 Reply Last reply
                    0
                    • T 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!

                      T Offline
                      T Offline
                      Tym
                      wrote on last edited by
                      #15

                      Thanks everyone for your help. Just to let you know, i ended up using encoding.default to get it working. Got a little lesson on .net text... Thanks again!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups