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. Data Conversion?

Data Conversion?

Scheduled Pinned Locked Moved C#
question
4 Posts 2 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.
  • D Offline
    D Offline
    dec82
    wrote on last edited by
    #1

    I have these code: protected bool EEWrite(UInt16 Address, byte Size, object[] ByteData) { bool Status = true; object[] Data = new object[4 + Size]; Data[0] = SerialNumber; Data[1] = (byte)0x80; Data[2] = (UInt16)Address; Data[3] = (byte)Size; for(int i = 4; i<=4+Size;i++) { Data[i] = (byte)ByteData[i - 4]; } Status = Device.Write( Data); return Status; } Then i 'd like to write a float number write_number as below: Status = EEWrite(0x9021, 1, ByteData); How would i convert a float write_number into ByteData? Thanks !

    M 1 Reply Last reply
    0
    • D dec82

      I have these code: protected bool EEWrite(UInt16 Address, byte Size, object[] ByteData) { bool Status = true; object[] Data = new object[4 + Size]; Data[0] = SerialNumber; Data[1] = (byte)0x80; Data[2] = (UInt16)Address; Data[3] = (byte)Size; for(int i = 4; i<=4+Size;i++) { Data[i] = (byte)ByteData[i - 4]; } Status = Device.Write( Data); return Status; } Then i 'd like to write a float number write_number as below: Status = EEWrite(0x9021, 1, ByteData); How would i convert a float write_number into ByteData? Thanks !

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      object[] byteData = BitConverter.GetBytes(write_number);

      I think that is what you want.

      Life goes very fast. Tomorrow, today is already yesterday.

      D 1 Reply Last reply
      0
      • M musefan

        object[] byteData = BitConverter.GetBytes(write_number);

        I think that is what you want.

        Life goes very fast. Tomorrow, today is already yesterday.

        D Offline
        D Offline
        dec82
        wrote on last edited by
        #3

        the code have error: cannot implicitly convert type 'byte[]' to 'object[]'

        M 1 Reply Last reply
        0
        • D dec82

          the code have error: cannot implicitly convert type 'byte[]' to 'object[]'

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          then assign in to a byte array and then if you need that as an object array loop through the byte array and pass each one to an object array.

          byte[] ba = BitConverter.GetBytes(write_number);
          object[] oa = new object[ba.Length];
          for(int i = 0; i < oa.length; i++)
          oa[i] = ba[i];

          Life goes very fast. Tomorrow, today is already yesterday.

          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