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