-
February 24th, 2012, 03:18 AM
#1
[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;
}
-
February 24th, 2012, 03:27 AM
#2
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.
-
February 24th, 2012, 03:38 AM
#3
Re: [Code proposal] Convert byte array in ASCII to int
Thanks
-
February 27th, 2012, 09:56 AM
#4
[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;
}
-
February 27th, 2012, 10:38 AM
#5
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.
-
February 27th, 2012, 12:17 PM
#6
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.
-
February 27th, 2012, 12:37 PM
#7
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
-
February 28th, 2012, 02:58 AM
#8
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
-
February 28th, 2012, 12:27 PM
#9
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.
-
February 29th, 2012, 03:44 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|