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. Have you ever used Buffer and/or BitConverter?

Have you ever used Buffer and/or BitConverter?

Scheduled Pinned Locked Moved C#
comquestion
3 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.
  • N Offline
    N Offline
    Nathan Ridley
    wrote on last edited by
    #1

    The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.

    namespace ConsoleApplication1
    {
      class Class1
      {
        [STAThread]
        static void Main(string[] args)
        {
          int int1 = 3;
          int int2 = 1000;
          byte[] b = new byte[16];
          Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8);
          //the following line crashes the program
          Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4);
        }
      }
    }
    

    NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]

    N J 2 Replies Last reply
    0
    • N Nathan Ridley

      The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.

      namespace ConsoleApplication1
      {
        class Class1
        {
          [STAThread]
          static void Main(string[] args)
          {
            int int1 = 3;
            int int2 = 1000;
            byte[] b = new byte[16];
            Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8);
            //the following line crashes the program
            Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4);
          }
        }
      }
      

      NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Try the following:

      using System;

      namespace ConsoleApplication1
      {
      class Class1
      {
      [STAThread]
      static void Main(string[] args)
      {
      int int1 = 3;
      int int2 = 1000;
      byte[] b = new byte[16];
      byte[] b2 = BitConverter.GetBytes(int1);
      byte[] b3 = BitConverter.GetBytes(int2);
      Buffer.BlockCopy(b2, 0, b, 0, b2.Length);
      Buffer.BlockCopy(b3, 0, b, 12, 4);
      }
      }
      }

      - Nick Parker
      My Blog | My Articles

      1 Reply Last reply
      0
      • N Nathan Ridley

        The last line of code below crashes my program. I am stumped as to why it's happening. I've double and triple checked the definitions for the BlockCopy and GetBytes functions and I still can't work out what I'm doing wrong. Any insight would be appreciated.

        namespace ConsoleApplication1
        {
          class Class1
          {
            [STAThread]
            static void Main(string[] args)
            {
              int int1 = 3;
              int int2 = 1000;
              byte[] b = new byte[16];
              Buffer.BlockCopy(BitConverter.GetBytes(int1), 0, b, 0, 8);
              //the following line crashes the program
              Buffer.BlockCopy(BitConverter.GetBytes(int2), 0, b, 12, 4);
            }
          }
        }
        

        NATHAN RIDLEY Web Application Developer email: nathan @ netlab.com.au [remove the spaces before and after the @ symbol]

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

        It's a weird problem, eh? The length of both arrays returned from GetBytes() is 4, so I think that it's actually the second-to-last line that's causing the problem with its length of 8. That code must be badly written in the current version of .NET: the second-to-last line above leaves the static stuff in Buffer somehow in an incorrect state, which is exposed by the subsequent call. Sorry I can't be of more help. Regards, Jeff Varszegi

        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