Results 1 to 3 of 3

Thread: Byte array to Integer conversion

  1. #1

    Thread Starter
    Member McBain2's Avatar
    Join Date
    Sep 2003
    Location
    UK, Glos.
    Posts
    60

    Byte array to Integer conversion

    Ok, I can do this by multipling up each byte from left to right but...

    I have an byte array and I want to extract a 4-byte Integer value from it, e.g.
    Dim Data() = {0,0,1,9} = 265 as an Integer

    What I want is simply to turn that array into a Dim I as Integer = the value of that array of data.

    Surely there is a simply way to convert this... since the data is in the actual format used by an Integer anyway!
    In pratice the Integer data will be within an array somewhere however - i.e. it wont just be a 4 byte array like the above example.

    Can't for the life of me see how to do this though...

    Anyone care to....

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    here is a byte array to char[]
    it might get u somewhere

    private char[] EncodeBytesToChars(byte[] data)
    {
    System.Text.Encoding encoding = System.Text.Encoding.Unicode;
    return encoding.GetChars(data);
    }

  3. #3
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    how is this?

    VB Code:
    1. Dim Data() As Byte = {0, 0, 1, 9} ' 256
    2.         Dim intValue As Integer
    3.         Data.Reverse(Data)
    4.         intValue = BitConverter.ToInt32(Data, 0)
    5.         MsgBox(intValue)
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width