Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to remove null byte from string in which i am searching for FIRST non zero byte. and after getting the first non ZERO byte keep all NULL bytes further. look at the example below.
Ex:
C++
QString myStr1 = "7f00000000000000";
QString myStr2 = "7f00000000008300";

Output Strings:
C++
myStr1 = 7f;   //all 7 Null Bytes are removed
myStr2 = 7f000000000083; //only the first Null byte is removed
Posted
Updated 14-Dec-15 23:13pm
v2
Comments
Leo Chapiro 15-Dec-15 5:16am    
Your intention is not clear, please explain more detailed! Your strings have 16 charachters, so if you devide them into 2 bytes you will get by the 1.string for example:
1.byte = "7f000000"
2.byte = "00000000"
So the desired solution would be "7f000000" and NOT "7f2?
XamBEE 15-Dec-15 5:25am    
I just want to remove all trailling zero.
Hexadecimal 4 Byte = 0x7f000000.
i want 0x7f for my application
Richard MacCutchan 15-Dec-15 5:32am    
These are strings of character, there are no null bytes in them. The easiest way to do this is just replace all trailing zeroes with null bytes '\0'. However, if you look at the QT documentation you may well find that QString has a method for trimming trailing characters.

From your previous posts I know that the input is originally an 8 byte wide buffer.

So why not use that (or a 64-bit value build from that) to perform your operations?

That is how programming works: Operating on binary data.

Strings should be used only for human readable user output, not to perform operations.

An don't try to solve every problem using Qt. That is a C++ based tool to provide platform independent GUIs. When it is not GUI or string related, use plain C or C++.

With a 64-bit value, the solution is:
C++
uint64_t val = getInputData(); // convert 8 byte to uint64
// EDIT2: Avoid infinite loop. Thanks to Shmuel Zang
if (val)
{
    // EDIT: This is obviously wrong
    //while (val & 0xff) 
    while (!(val & 0xff))
        val >>= 8;
}
 
Share this answer
 
v3
Comments
XamBEE 15-Dec-15 6:13am    
quint64 val = 0x7b00000000440088;
for (int i = 0; i < 8; i++)
{
qbaa[i] = static_cast<char>(val & 0xFF);
if(qbaa.toHex()[i]!=0)
{
fieldArr.append(qbaa.toHex()[i]);
}
val >>= 8;
}
Jochen Arndt 15-Dec-15 6:31am    
The code is totally wrong (may even generate an access violation).

It would be better if you tell us what you finally want to achieve (what to do with your input data).

That is another part of how programming works:
Define what should be done (e.g. operating on a specified type of input values and store them finally again as specified type). Then think about how that can be implemented.

So you have an input of 8 bytes stored in an array (BYTE MsgData.DATA[8] if I remember correctly).
Questions:
- Where is the LSB (least signficand byte) stored? At offset 0 or 7?
- What should be done with this data?
- What data type(s) should be finally used when storing the result(s)?
XamBEE 15-Dec-15 6:45am    
YES its totally wrong.
I am reading String myStr1 = "7f00000000000000"; from txt file. and after removing all trailing zeros. Quint64 strdata= 7f.
your proposed solution is the smartest solution i think and working as well giving me output EXACTLY what i am looking for BUT didnt get fully how its working. would be nice if you explain it bit.
Jochen Arndt 15-Dec-15 6:57am    
If the input is a QString:
qulonglong val = myStr1.toULongLong(NULL, 16);
while (!(val & 0xff)) val >>= 8;

If you need the result as hex QString again:
QString myStr2 = QString::number(val, 16);

How it works:
Start value is 0x7f00000000000000
val & 0xff: 0x0000000000000000 -> zero, continue
val >>= 8: 0x007f000000000000
This is repeated until the value is 0x7f.
Shmuel Zang 15-Dec-15 7:36am    
5'ed. I also provided a solution using a string (as the OP asked) but, using numbers it's more efficient.
Anyway, I'd change the loop's condition to !(val & 0xff) && val != 0. Otherwise, in case of val == 0, it'll cause an infinite loop...
Try this:

C++
String myStr1 = "7f00000000000000";
myStr1 .remove( QRegExp("[0]*$") );
 
Share this answer
 
v2

I guess you mean to an hexadecimal representation of bytes. What about searching for the last occurrence of a zero byte (from the end of the string) and, getting the relevant length of the string (from the begin of the string)? Something like:


C++
QString removeZeroBytes(const QString& src)
{
    int len = src.length();

    while (len > 1)
    {
        if (src.midRef(len - 2, 2) == "00")
        {
            len -= 2;
        }
        else
        {
            break;
        }
    }

    return src.left(len);
}

int main()
{
    QString myStr1 = "7f00000000000000";
    QString myStr2 = "7f00000000008300";

    QString s1 = removeZeroBytes(myStr1);
    QString s2 = removeZeroBytes(myStr2);

    return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900