如何从C ++ / QT中的String中删除NULL字节 [英] How to remove NULL byte from String in C++/QT

查看:107
本文介绍了如何从C ++ / QT中的String中删除NULL字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字符串中删除空字节,我在其中搜索FIRST非零字节。在获得第一个非ZERO字节后,进一步保留所有NULL字节。看下面的例子。

Ex:

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:

QString myStr1 = "7f00000000000000";
QString myStr2 = "7f00000000008300";



输出字符串:


Output Strings:

myStr1 = 7f;   //all 7 Null Bytes are removed
myStr2 = 7f000000000083; //only the first Null byte is removed

推荐答案

从你之前的帖子我知道输入最初是一个8字节宽的缓冲区。



那么为什么不使用它(或者从中构建64位值来执行操作?



这就是编程的工作原理:对二进制数据进行操作。



字符串只能用于人类可读的用户输出,不能用于执行操作。



尝试用Qt解决每个问题。这是一个基于C ++的工具,提供独立于平台的GUI。当它不是GUI或字符串相关时,使用普通的C或C ++。



使用64位值,解决方案是:

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:
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;
}


试试这个:



Try this:

String myStr1 = "7f00000000000000";
myStr1 .remove( QRegExp("[0]*


));
") );


这篇关于如何从C ++ / QT中的String中删除NULL字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆