//
//  NumberUtils.hpp
//  qlibc++
//
//  Created by 胡涛 on 2017/11/16.
//  Copyright © 2017年 胡涛. All rights reserved.
//

#ifndef NumberUtils_hpp
#define NumberUtils_hpp

#include <cstdio>
#include <string>
#include <cstring>
#include "ByteUtils.h"
#include "StringUtils.hpp"
#include <jebase/config.h>

static  const char* sval_hex_sign_lower = "0x";
static  const char* sval_hex_sign = "0X";
static  const size_t sval_hex_sign_len = strlen(sval_hex_sign);

static  const char* sval_bit_sign_lower = "0b";
static  const char* sval_bit_sign = "0B";
static  const size_t sval_bit_sign_len = strlen(sval_bit_sign);

class JE_API NumberUtils{

public:
     static  int  randomIntger(int base ,int max);
    
     static  double   randomFloat(double base, double max);

     static  double  vrandom();

     static  size_t hexStrToBytes(const std::string &hexstr, unsigned char **buff);
     //此函数仅用于jedp，日常转换勿使用
     static  size_t hexStrToBytes(const std::string &srcStr, QByte *targetBuff, size_t bufflen);
     //使用此函数进行转换
     static  size_t hexStrToBytesRaw(std::string &hexstr, QByte *targetBuff, size_t bufflen);

     static  void bytesToHexStr(const unsigned char *buff, size_t bufflen, std::string& str);
     static  std::string  bytesToHexStr(const unsigned char *buff, size_t bufflen);

     static  size_t bitStrToBytes(const std::string &hexstr, unsigned char **buff);

     static  void bytesToBitStr(const unsigned char *buff, size_t bufflen, std::string& str);
     static  std::string  bytesToBitStr(const unsigned char *buff, size_t bufflen);

     //注意，此2函数仅用于jedp，其余场景请勿使用。正常使用请使用 bytesToHexStrOrderedPack
     static  void bytesToHexStrPacked(const unsigned char *buff, size_t bufflen, std::string& str);
     static  std::string  bytesToHexStrPacked(const unsigned char *buff, size_t bufflen);
     // 一般使用如下函数
     static  void bytesToHexStrOrderedPack(const unsigned char *buff, size_t bufflen, std::string& str, bool lower=false, bool noHeader = false);
     static  std::string  bytesToHexStrOrderedPack(const unsigned char *buff, size_t bufflen, bool lower=false, bool noHeader = false);

     static  int  stringToIntWithFmtCheck(const std::string &valstr, bool& ok);
     static  int  stringToInt(const std::string &valstr);
     static  int  hexStringToInt(const std::string& hexStr);


    static  unsigned int  stringToUInt(const std::string &valstr);

     static  long long  stringToLongLong(const std::string &valstr);
     static  unsigned long long stringToULLong(const std::string &valstr);


    static size_t hexStrByteLen(const std::string &hexStr);

    static std::string decIntToHexStr(long long num);


    static std::string hexStr(std::string hexstr);

private:
    static  size_t hexStrPackedToBuffer(std::string srcStr, QByte *buff, size_t size);
    static  size_t hexStrPackedToBufferOrder(std::string srcStr, QByte *buff, size_t size);
    static  size_t hexStrFormattedToBuffer(QStringList& sl, QByte *buff, size_t bufflen);

#define QHexStr(buff, len)\
    NumberUtils::bytesToHexStr(buff, len).c_str()
};


#endif /* NumberUtils_hpp */
