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 array into integer?

Converting Byte array into integer?

Scheduled Pinned Locked Moved C#
questiondata-structureshelp
3 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.
  • K Offline
    K Offline
    Kiran Satish
    wrote on last edited by
    #1

    Hello all, Looks like this is a basic question that I somehow is not able to figure it out properly. The method I am using works for Hex numbers that does not contain 0 in its lower byte. I guess I have to use a different approach to get around this problem, which I am not able to quite figure it out. I am reading values off of an IC which pushes out serial data in bits and the total length of the output is always 16bits which are read into two bytes. So, when the value is 255 its fine when I convert it into integer. But when the output from the chip is equivalent to 256, it turns out to be '1' in Upper byte and '0' in lower byte which is not being converted to 256 but rather to 16 using the following code.

    string hexValue = In[0].ToString("X") + In[1].ToString("X");
    int hscandacvalue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

    Any suggestions? thanks

    PKNT

    L 1 Reply Last reply
    0
    • K Kiran Satish

      Hello all, Looks like this is a basic question that I somehow is not able to figure it out properly. The method I am using works for Hex numbers that does not contain 0 in its lower byte. I guess I have to use a different approach to get around this problem, which I am not able to quite figure it out. I am reading values off of an IC which pushes out serial data in bits and the total length of the output is always 16bits which are read into two bytes. So, when the value is 255 its fine when I convert it into integer. But when the output from the chip is equivalent to 256, it turns out to be '1' in Upper byte and '0' in lower byte which is not being converted to 256 but rather to 16 using the following code.

      string hexValue = In[0].ToString("X") + In[1].ToString("X");
      int hscandacvalue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

      Any suggestions? thanks

      PKNT

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The problem with this is that it turns eg 0x01 (as a byte) into the string 1, and then the leading zero is lost. That's a problem, because you're concatenating in front of it. You could fix that by using X2, but don't. But you don't need any of this weird string business anyway, you can simply use math (lowbyte + 256 * highbyte)

      K 1 Reply Last reply
      0
      • L Lost User

        The problem with this is that it turns eg 0x01 (as a byte) into the string 1, and then the leading zero is lost. That's a problem, because you're concatenating in front of it. You could fix that by using X2, but don't. But you don't need any of this weird string business anyway, you can simply use math (lowbyte + 256 * highbyte)

        K Offline
        K Offline
        Kiran Satish
        wrote on last edited by
        #3

        Thanks Harold, that works great.

        PKNT

        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