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. Converting byte[] to Hex String;

Converting byte[] to Hex String;

Scheduled Pinned Locked Moved C#
databasedata-structureshelp
8 Posts 3 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.
  • B Offline
    B Offline
    Bo Hunter
    wrote on last edited by
    #1

    I know that this is possible.

    internal static string ByteArrayToHexString( byte[] buf )
    {
    StringBuilder sb = new StringBuilder( buf.Length );
    for ( int i = 0; i != buf.Length; ++i )
    {
    sb.Append( buf[i].ToString( "X2" ) );
    }
    return ( sb.ToString() );
    }

    But I found this code in the BCL and was just trying to make it work. The trouble I am having is in the lowwer while loop. From what I can see is it is supposed to index into the s_acharval array that contains the valid hex digits. But I have had no luck in figuring it out.

    internal unsafe static string ByteArrayToHexString( byte[] buf, int iLen )
    {
    char[] chs1 = s_acharval;
    if ( chs1 == null )
    {
    chs1 = new char[16];
    int i = (int)chs1.Length;
    while ( --i >= 0 )
    {
    if ( i < 10 )
    {
    chs1[i] = (char)( 48 + i );
    }
    else
    {
    chs1[i] = (char)( 65 + ( i - 10 ) );
    }
    }
    s_acharval = chs1;
    }
    if ( buf == null )
    {
    return null;
    }
    if ( iLen == 0 )
    {
    iLen = (int)buf.Length;
    }
    char[] chs2 = new char[(uint)(iLen * 2)];
    fixed ( char* ch1 = &chs2[0])
    {
    fixed ( char* ch2 = &chs1[0])
    {
    fixed ( byte* b1 = &buf[0] )
    {
    char* ch3 = ch1;
    byte* b2 = b1;
    while ( --iLen >= 0 )
    {
    *ch3++ = *( ch2 + ( 2 * (( *b2 & 240 ) >> 4 )));
    *ch3++ = *( ch2 + ( 2 * ( *b2 & 15 )));
    b2++;
    }
    }
    }
    }

    return new String( chs2 );
    

    }

    Any help would be greatly appreciated. Thank You Bo Hunter

    H 1 Reply Last reply
    0
    • B Bo Hunter

      I know that this is possible.

      internal static string ByteArrayToHexString( byte[] buf )
      {
      StringBuilder sb = new StringBuilder( buf.Length );
      for ( int i = 0; i != buf.Length; ++i )
      {
      sb.Append( buf[i].ToString( "X2" ) );
      }
      return ( sb.ToString() );
      }

      But I found this code in the BCL and was just trying to make it work. The trouble I am having is in the lowwer while loop. From what I can see is it is supposed to index into the s_acharval array that contains the valid hex digits. But I have had no luck in figuring it out.

      internal unsafe static string ByteArrayToHexString( byte[] buf, int iLen )
      {
      char[] chs1 = s_acharval;
      if ( chs1 == null )
      {
      chs1 = new char[16];
      int i = (int)chs1.Length;
      while ( --i >= 0 )
      {
      if ( i < 10 )
      {
      chs1[i] = (char)( 48 + i );
      }
      else
      {
      chs1[i] = (char)( 65 + ( i - 10 ) );
      }
      }
      s_acharval = chs1;
      }
      if ( buf == null )
      {
      return null;
      }
      if ( iLen == 0 )
      {
      iLen = (int)buf.Length;
      }
      char[] chs2 = new char[(uint)(iLen * 2)];
      fixed ( char* ch1 = &chs2[0])
      {
      fixed ( char* ch2 = &chs1[0])
      {
      fixed ( byte* b1 = &buf[0] )
      {
      char* ch3 = ch1;
      byte* b2 = b1;
      while ( --iLen >= 0 )
      {
      *ch3++ = *( ch2 + ( 2 * (( *b2 & 240 ) >> 4 )));
      *ch3++ = *( ch2 + ( 2 * ( *b2 & 15 )));
      b2++;
      }
      }
      }
      }

      return new String( chs2 );
      

      }

      Any help would be greatly appreciated. Thank You Bo Hunter

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      So what exactly are you trying to figure out? If you don't understand how the code works, I suggest you first read-up on the fixed statement (which pins an object in memory, preventing it from being relocated by the GC). The rest is simple pointer math.

      Microsoft MVP, Visual C# My Articles

      B 1 Reply Last reply
      0
      • H Heath Stewart

        So what exactly are you trying to figure out? If you don't understand how the code works, I suggest you first read-up on the fixed statement (which pins an object in memory, preventing it from being relocated by the GC). The rest is simple pointer math.

        Microsoft MVP, Visual C# My Articles

        B Offline
        B Offline
        Bo Hunter
        wrote on last edited by
        #3

        I should have been more pacific. What I ment by the part I am having trouble with is the last while loop is it does not work. These lines are the problem.

        *ch3++ = *( ch2 + ( 2 * (( *b2 & 240 ) >> 4 )));
        *ch3++ = *( ch2 + ( 2 * ( *b2 & 15 )));
        b2++;

        What I am going by is the hash returned for a certain byte[] does not match the result from the same function with the same byte[] in the BCL. Thank You Bo Hunter

        H K 2 Replies Last reply
        0
        • B Bo Hunter

          I should have been more pacific. What I ment by the part I am having trouble with is the last while loop is it does not work. These lines are the problem.

          *ch3++ = *( ch2 + ( 2 * (( *b2 & 240 ) >> 4 )));
          *ch3++ = *( ch2 + ( 2 * ( *b2 & 15 )));
          b2++;

          What I am going by is the hash returned for a certain byte[] does not match the result from the same function with the same byte[] in the BCL. Thank You Bo Hunter

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          "Specific". "Pacific" is the worlds largest ocean. Also, sayinging "it does not work" doesn't help me one bit. What doesn't work? Be more specific. Do you tell your doctor that you don't feel well and leave it at that? A byte is a byte is a byte. A byte is 8 bits (currently; I doubt that will ever change). On some platforms, these are big endian and some art little endian, but the .NET Framework takes care of that detail for you. Whatever the case is, the BCL is correct. If you don't understand pointer math, I suggest you just use byte.ToString("x2"); otherwise, please be more specific about what the problem is. I - or any other regular here - am not going to debug your code for you, but we'll try to help you if you be more specific and tell us what's wrong.

          Microsoft MVP, Visual C# My Articles

          B K 2 Replies Last reply
          0
          • H Heath Stewart

            "Specific". "Pacific" is the worlds largest ocean. Also, sayinging "it does not work" doesn't help me one bit. What doesn't work? Be more specific. Do you tell your doctor that you don't feel well and leave it at that? A byte is a byte is a byte. A byte is 8 bits (currently; I doubt that will ever change). On some platforms, these are big endian and some art little endian, but the .NET Framework takes care of that detail for you. Whatever the case is, the BCL is correct. If you don't understand pointer math, I suggest you just use byte.ToString("x2"); otherwise, please be more specific about what the problem is. I - or any other regular here - am not going to debug your code for you, but we'll try to help you if you be more specific and tell us what's wrong.

            Microsoft MVP, Visual C# My Articles

            B Offline
            B Offline
            Bo Hunter
            wrote on last edited by
            #5

            You know something Heath; don’t answer any more of my post. You have a big chip on your shoulder. I have never seen you correct someone's Spelling before, all you are doing is being a smart ass. It is because of me posting some of your previous posts when you start bashing people for not doing this and not doing that. What is your problem? I could understand that being vague or something but to start a post with ("Specific". "Pacific" is the world’s largest ocean.) Is nothing more than the arrogant, self absorbed, inconsiderate prick that you are. Now did I misspell anything this time? This is what Heath Stewart thinks about Certifications. Heath Stewart said "Personally, I am not certified because I really don't want to be. I know a large number of people with certifications that know crap. They studied for the tests, memorized a few things, took the tests (sometimes a couple times) and got their certs. They're still idiots."

            1 Reply Last reply
            0
            • B Bo Hunter

              I should have been more pacific. What I ment by the part I am having trouble with is the last while loop is it does not work. These lines are the problem.

              *ch3++ = *( ch2 + ( 2 * (( *b2 & 240 ) >> 4 )));
              *ch3++ = *( ch2 + ( 2 * ( *b2 & 15 )));
              b2++;

              What I am going by is the hash returned for a certain byte[] does not match the result from the same function with the same byte[] in the BCL. Thank You Bo Hunter

              K Offline
              K Offline
              Karl 2000
              wrote on last edited by
              #6

              Hi Bo, First off let me apologize for the rudeness of others. This is the place to post questions like these, and you did show us in the code exactly where the problem was occurring. I believe that replacing your troubled lines with the following might fix your problem. *ch3++ = *( ch2 + ( 1 * (( *b2 & 240 ) >> 4 ))); *ch3++ = *( ch2 + ( 1 * ( *b2 & 15 ))); b2++; Best of Luck! Karl Baum CEO of KGB Technologies Specializing in custom software development.

              1 Reply Last reply
              0
              • H Heath Stewart

                "Specific". "Pacific" is the worlds largest ocean. Also, sayinging "it does not work" doesn't help me one bit. What doesn't work? Be more specific. Do you tell your doctor that you don't feel well and leave it at that? A byte is a byte is a byte. A byte is 8 bits (currently; I doubt that will ever change). On some platforms, these are big endian and some art little endian, but the .NET Framework takes care of that detail for you. Whatever the case is, the BCL is correct. If you don't understand pointer math, I suggest you just use byte.ToString("x2"); otherwise, please be more specific about what the problem is. I - or any other regular here - am not going to debug your code for you, but we'll try to help you if you be more specific and tell us what's wrong.

                Microsoft MVP, Visual C# My Articles

                K Offline
                K Offline
                Karl 2000
                wrote on last edited by
                #7

                Heath, I agree with Bo. Please do not waste our time with negative and counterproductive rants. Karl

                H 1 Reply Last reply
                0
                • K Karl 2000

                  Heath, I agree with Bo. Please do not waste our time with negative and counterproductive rants. Karl

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Please don't waste your time? I reply to about 1,000 posts per month in this forum - I don't see your name as a reply to even a fraction of as much. It's people that don't state what the problem is and expect the other regulars and me to debug their code for them that wastes time. Saying "it doesn't work" is a waste of the developers' time who spend their time in this forum answering questions on a regular basis, and it is happening more often lately.

                  Microsoft MVP, Visual C# My Articles

                  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