vocab.cpp

来自「一个语言识别引擎」· C++ 代码 · 共 51 行

CPP
51
字号
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

/*
 * Copyright (C) 2006 Paul Fitzpatrick
 * CopyPolicy: Released under the terms of the GNU GPL v2.0.
 *
 */


#include <yarp/String.h>
#include <yarp/os/Vocab.h>

using namespace yarp::os;
using namespace yarp;

NetInt32 Vocab::encode(const char *str) {
    String s(str);
    char a = '\0';
    char b = '\0';
    char c = '\0';
    char d = '\0';
    if (s.length()>=1) {
        a = s[0];
        if (s.length()>=2) {
            b = s[1];
            if (s.length()>=3) {
                c = s[2];
                if (s.length()>=4) {
                    d = s[3];
                }
            }
        }
    }
    return VOCAB(a,b,c,d);
}


ConstString Vocab::decode(NetInt32 code) {
    String s;
    for (int i=0; i<4; i++) {
        int ch = code%256;
        if (ch>0) {
            s += ((char)ch);
        }
        code /= 256;
    }
    return ConstString(s.c_str());
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?