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. Visual Basic
  4. How to BitConverter.GetBytes() return 2 digits only

How to BitConverter.GetBytes() return 2 digits only

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
4 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.
  • E Offline
    E Offline
    EngrImad
    wrote on last edited by
    #1

    Hello every one simple code as follow

    Dim MyInt As Int16 = 12
    Dim result As Byte() =BitConverter.GetBytes(MyInt)
    Console.WriteLine(BitConverter.ToString(result).Length)
    Console.WriteLine(BitConverter.ToString(result))

    this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance

    L E D 3 Replies Last reply
    0
    • E EngrImad

      Hello every one simple code as follow

      Dim MyInt As Int16 = 12
      Dim result As Byte() =BitConverter.GetBytes(MyInt)
      Console.WriteLine(BitConverter.ToString(result).Length)
      Console.WriteLine(BitConverter.ToString(result))

      this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance

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

      It really depends on what you are trying to do, and why you need to convert a number to a hexadecimal string. You could just use the 'X' format string to print the value, which would give you the correct answer.

      1 Reply Last reply
      0
      • E EngrImad

        Hello every one simple code as follow

        Dim MyInt As Int16 = 12
        Dim result As Byte() =BitConverter.GetBytes(MyInt)
        Console.WriteLine(BitConverter.ToString(result).Length)
        Console.WriteLine(BitConverter.ToString(result))

        this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance

        E Offline
        E Offline
        EngrImad
        wrote on last edited by
        #3

        I solved like follows:-

        Dim MyInt As Int16 = 12
        Dim result (0) As Byte
        result (0) = Convert.ToByte(MyInt)
        Console.WriteLine(BitConverter.ToString(result))

        thanks everyone again

        1 Reply Last reply
        0
        • E EngrImad

          Hello every one simple code as follow

          Dim MyInt As Int16 = 12
          Dim result As Byte() =BitConverter.GetBytes(MyInt)
          Console.WriteLine(BitConverter.ToString(result).Length)
          Console.WriteLine(BitConverter.ToString(result))

          this will return as follow 5 0C-00 Just I want to get 2 0C (length 2 only) and return (0C) only I try to change "MyInt" Declare type from Int16 to all available types but did not work, also i dont need to subtract result as string, i need result as byte() type any advise please ? I am using VS2019 thanks in advance

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          An int16 (or 'short') is a two-byte value. Intel processors are "little-endian", meaning the last byte (least significant) is stored first and the first byte (most significant) is stored last. In your example, your two byte value, (12) 0x000C, is stored in memory like this: 0x0C, 0x00. So that's why you're getting the bytes in the order you are. BitConverter is returning the bytes in the order they appear in memory. If you were running the exact same code on a processor that is big-endian, the bytes would show up in the array in the order opposite of what you're seeing now. Now, having said all that, if you're looking for just getting a string for the hexadecimal representation of your value, all you have to do is this:

          Dim myInt As Int16 = 12
          result = String.Format("X", myInt)

          If you're dealing with values that will never be greater than 255, why are you using an Int16? Why not just use Byte instead?

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          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