Hi, I'm trying to rewrite some functions from C# to Qt, but something is going wrong. I dont know why...
Could somebody look in my code please ?
Function should convert byte to uint:
Orginal C# function:
public static String ByteToString(byte[] crc)
{
String ret = "";
if(crc != null){
ret = ((int)crc[0]).ToString("X2");
ret += ((int)crc[1]).ToString("X2");
ret += ((int)crc[2]).ToString("X2");
ret += ((int)crc[3]).ToString("X2");
}
return ret;
}
public static uint ByteTouInt(byte[] crc){
if(crc != null) {
return Convert.ToUInt32(Tools.ByteToString(crc), 16);
}else{
return 0;
}
return 0;
}
public static String ByteToString(byte[] crc)
{
String ret = "";
if(crc != null){
ret = ((int)crc[0]).ToString("X2");
ret += ((int)crc[1]).ToString("X2");
ret += ((int)crc[2]).ToString("X2");
ret += ((int)crc[3]).ToString("X2");
}
return ret;
}
public static uint ByteTouInt(byte[] crc){
if(crc != null) {
return Convert.ToUInt32(Tools.ByteToString(crc), 16);
}else{
return 0;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
My function in Qt (First idea)
QString Tools
::byteToString(byte
*crc
) {
if(sizeof crc < 4 || crc == NULL) {
return "bad";
}
for(int i = 0; i < 4; i++) {
crc_string
+= (QString::number((int)crc
[i
],
16).
length() == 1) ?
"0" + QString::number((int)crc
[i
],
16) : QString::number((int)crc
[i
],
16);
}
return crc_string.toUpper();
}
inline uint byteToUInt(byte *crc) {
return byteToString(crc).toUInt(NULL, 16);
}
QString Tools::byteToString(byte *crc)
{
QString crc_string = "";
if(sizeof crc < 4 || crc == NULL) {
return "bad";
}
for(int i = 0; i < 4; i++) {
crc_string += (QString::number((int)crc[i], 16).length() == 1) ? "0" + QString::number((int)crc[i], 16) : QString::number((int)crc[i], 16);
}
return crc_string.toUpper();
}
inline uint byteToUInt(byte *crc) {
return byteToString(crc).toUInt(NULL, 16);
}
To copy to clipboard, switch view to plain text mode
Second idea...
inline uint Tools::byteToUInt(byte *b)
{
uint val;
ba.append(((const char*) b));
//val = (ba[1] << 8) | ba[0];
memcpy(&val, ba.constData(), 2);
return val;
}
inline uint Tools::byteToUInt(byte *b)
{
uint val;
QByteArray ba;
ba.append(((const char*) b));
//val = (ba[1] << 8) | ba[0];
memcpy(&val, ba.constData(), 2);
return val;
}
To copy to clipboard, switch view to plain text mode
Function returns different values, I'm not a author of C# code, so I've got problems to convert it properly.
To compile C# code I used this online IDE/compiler: http://www.tutorialspoint.com/compile_csharp_online.php
Thank You for any help and suggestions.
Bookmarks