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. Byte Array to uint (Hex Decimal Value) Little help [I Solved It]

Byte Array to uint (Hex Decimal Value) Little help [I Solved It]

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
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.
  • X Offline
    X Offline
    xEvOx
    wrote on last edited by
    #1

    Code :

    public static uint GetUintFrombyte(byte[] ByteArry)
    {
    string Array = ByteArray.ToString(); /OR/ string Array = BitConverter.ToString(ByteArray);
    int Value = int.Parse(Array, NumberStyles.HexNumber);
    uint Value2 = Value;
    return Value2;
    }

    Now here i read 4 bytes Binary reader : byte[] Hex = br.ReadBytes(4); Hex Now ='s 00000001 00000001 to a int value should = 1 then transfering it to a uint , but it isnt seemingly doing it . Error comes up as on int value "Error : Invalid Format " Any Help ? Thanks

    modified on Sunday, February 7, 2010 11:58 PM

    X 1 Reply Last reply
    0
    • X xEvOx

      Code :

      public static uint GetUintFrombyte(byte[] ByteArry)
      {
      string Array = ByteArray.ToString(); /OR/ string Array = BitConverter.ToString(ByteArray);
      int Value = int.Parse(Array, NumberStyles.HexNumber);
      uint Value2 = Value;
      return Value2;
      }

      Now here i read 4 bytes Binary reader : byte[] Hex = br.ReadBytes(4); Hex Now ='s 00000001 00000001 to a int value should = 1 then transfering it to a uint , but it isnt seemingly doing it . Error comes up as on int value "Error : Invalid Format " Any Help ? Thanks

      modified on Sunday, February 7, 2010 11:58 PM

      X Offline
      X Offline
      xEvOx
      wrote on last edited by
      #2

      I solved it , Here is the code :

      (new Class)
      public static string HexToAscii(byte[] hex)
      {
      string ascii = "";
      for (int i = 0; i < hex.Length; i++)
      {
      string temp = hex[i].ToString("X");
      if (temp.Length == 1)
      temp = "0" + temp;
      ascii += temp;
      }
      return ascii;
      }

      public static uint BytetoUint(byte[] Data)
      {
      string Array = Conversions.HexToAscii(Data);
      int i = int.Parse(Array, NumberStyles.HexNumber);
      return Convert.ToUInt32(i);
      }

      L 1 Reply Last reply
      0
      • X xEvOx

        I solved it , Here is the code :

        (new Class)
        public static string HexToAscii(byte[] hex)
        {
        string ascii = "";
        for (int i = 0; i < hex.Length; i++)
        {
        string temp = hex[i].ToString("X");
        if (temp.Length == 1)
        temp = "0" + temp;
        ascii += temp;
        }
        return ascii;
        }

        public static uint BytetoUint(byte[] Data)
        {
        string Array = Conversions.HexToAscii(Data);
        int i = int.Parse(Array, NumberStyles.HexNumber);
        return Convert.ToUInt32(i);
        }

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        That is some terrible code.

        xEvOx wrote:

        public static string HexToAscii(byte[] hex)

        bad method name, nothing is hex unless it is a string holding the hex representation of something. should be ToHexString(byte[] bytes)

        xEvOx wrote:

        string temp = hex[i].ToString("X"); if (temp.Length == 1) temp = "0" + temp;

        use ToString("X2") to always get at least two characters

        xEvOx wrote:

        string ascii = "";

        in /NET all strings use Unicode, not ASCII.

        xEvOx wrote:

        public static uint BytetoUint(byte[] Data)

        another bad method name, it does not convert a byte, in converts a byte array. and a very inefficient way to do things: you have numbers in the byte array, turn them into a string in order to extract a numeric value. One should never do that, just work with the numbers themselves, something like (untested):

        public static uint ToUint(byte[] bytes) {
        uint result=0;
        foreach(byte b in bytes) result=(result<<8) | b;
        return result;
        }

        :~

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        [The QA section does it automatically now, I hope we soon get it on regular forums as well]


        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