CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    [RESOLVED] [Code proposal] Convert byte array in ASCII to int

    Hello,

    This converts ASCII data to the equivalent int as if we had converted the string into the meaning int.
    Comments and feedback appreciated, thanks !
    Code:
            public int iByteArrayASCIIToInt(byte[] bTable, int iStart, int iEnd)
            {
                int retval;
                int i;
                int iNumberOfElements;
                iNumberOfElements = iEnd - iStart;
                retval = 0;
                for (i = 0; i <= iNumberOfElements; i++)
                {
                    retval += (bTable[iStart + i] - 0x30) * (int)Math.Pow(10, iNumberOfElements - i);
                }
                return retval;
            }

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: [Code proposal] Convert byte array in ASCII to int

    Using the internal libraries, you can do it quicker:

    Code:
    string asciiString = System.Text.Encoding.ASCII.getString(bTable);
    return Int32.Parse(asciiString);
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: [Code proposal] Convert byte array in ASCII to int

    Thanks

  4. #4
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    [Code proposal] Convert Hex values in ASCII in byte

    Hello again people, this time i had a file with hex values typped in ASCII but i needed them in byte array so I made this. Any feedback is appreciated

    Code:
            
    private byte castASCIIByte(byte b1,byte b2)
            {
                byte retval;
                retval = 0;
    
                if ((b1 > 0x2F)&&(b1<0x3A))
                {
                    retval =(byte) ((b1 - 0x30) * 16);
                }
                else if ((b1 > 0x40) && (b1 < 0x47))
                {
                    retval = (byte)((b1 - 55) * 16);
                }
                else if ((b1 > 0x60) && (b1 < 0x67))
                {
                    retval = (byte)((b1 - 87) * 16);
                }
    
                if ((b2 > 0x2F) && (b2 < 0x3A))
                {
                    retval += (byte)(b2 - 0x30);
                }
                else if ((b2 > 0x40) && (b2 < 0x47))
                {
                    retval += (byte)(b2 - 55);
                }
                else if ((b2 > 0x60) && (b2 < 0x67))
                {
                    retval += (byte)(b2 - 87);
                }
                return retval;
            }

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [Code proposal] Convert Hex values in ASCII in byte

    If it is a file and you need to get it into a byte array the simple way is to just read it into a byte array.

    create a filestream pointed to the file
    create a binaryreader pointed to the file stream
    using the binaryreader readallbytes
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: [Code proposal] Convert byte array in ASCII to int

    Do believe we could keep these two related.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    May 2002
    Location
    Boston
    Posts
    67

    Re: [Code proposal] Convert byte array in ASCII to int

    Hi,

    You can use the Convert Class and the BitConverter classes

    Code:
    int checkVal;
    
    checkVal = Convert.ToInt16(String.Format("{0:F0}", pageByte), 16) ; //base 16
    checkVal = Convert.ToInt16(String.Format("{0:F0}", lsb), 10); //base 10
    
    Trace.WriteLine("Checkval = " + checkVal);
    
    int checkSum;
                                  
    checkSum = 256 - checkVal;
    strcheckSum = BitConverter.ToString(BitConverter.GetBytes(checkSum));
    Curt

  8. #8
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: [Code proposal] Convert byte array in ASCII to int

    Thanks for your comments

    What I was looking for is for that :
    1) I have a file like this
    "F662A4"
    Means that in hex the file is like this :
    00000000h: 46 36 36 32 41 34 ; F662A4
    2) What i wanted is a byte[3] to look like this
    byte[0] = F6
    byte[1] = 62
    byte[2] = A4

  9. #9
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: [Code proposal] Convert byte array in ASCII to int

    You can parse them like this:

    Code:
    string hexString = "F4";
    byte parsed = Byte.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
    
    //Parsed now contains 0xF4 (=244)
    This is based on the MSDN article showing how to do it for int (see link), but I verified it works for byte as well.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: [Code proposal] Convert byte array in ASCII to int

    Yay thanks, helps me a lot

Tags for this Thread

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

Featured