【问题标题】:convert unsigned long to wchar_t将 unsigned long 转换为 wchar_t
【发布时间】:2016-04-11 09:14:00
【问题描述】:

我想将一个无符号值复制到wchar_t 数组。 并使用这个

unsigned long lValue = <value>//value 

wchar_t wszBuffer[256] = L"";
::swprintf_s( wszBuffer, _countof(wszBuffer), wszFormat, lValue );

它不适用于大于 2147483647 的长值。 有什么解决办法?

【问题讨论】:

  • wszFormat 的值是多少?我希望"%lu"
  • 谢谢..这是错误的。我正在使用 %d。

标签: wchar-t


【解决方案1】:

认为 wchar_t 被保存为 2 16bit int 作为 UTF-16 格式。 除非必须,否则不会使用它。下面的结构将任何章程存储在单个 uint32_t(c++ 中的 32 位整数)中。 但是如果你需要一个无符号长(64 位整数)数组, 我会用这样的向量创建一个:

std::vector<uint64_t> array;

关注 32 位或更少

 full file location: 
 https://github.com/NashBean/iBS_LIB/blob/master/iBS_Header.h

//-----------------------------------------------
//uchar = uint made to hold any char of the world
//-----------------------------------------------
 struct uchar
{
    uchar():value(0){}; 
    uchar(int v):value((uint32_t)v){}; 
    uchar(long v):value((uint32_t)v){}; 
    uchar(uint32_t v):value(v){}; 
    uchar(uint v):value(v.get()){}; 
    uchar(char v):value((uint32_t)v){}; 
    uchar(uchar const &v):value(v.value){}; 
    uchar(wchar_t wch):value(decode(wch)){}; 

    uchar& operator=(int unicode){ 
     set((uint32_t)unicode); 
return *this; };
    uchar& operator=(uint32_t unicode){ set(unicode); 
return *this; };
    uchar& operator=(uint unicode) { set(unicode.get()); 
return *this; };
    uchar& operator=(char ch) { set((uint32_t)ch); return *this; };
    uchar& operator=(uchar uch) { set(uch.get()); return *this; };
    uchar& operator=(wchar_t wch) { set(decode(wch).get()); 
return *this; };// use of Uchar.h

    bool operator==(int i) { return (value == i); };
    bool operator==(uint32_t unicode) { return (value == unicode); };
    bool operator==(uint unicode) { return (value == unicode); };
    bool operator==(char c) { return (value == c); };
    bool operator==(uchar uch) { return (value == uch.value); };

    uint32_t get() { return value.get(); };
    void set(uint32_t v) { value = v; };

private:    
    uint value;
};

【讨论】:

  • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 1970-01-01
  • 2016-04-19
  • 1970-01-01
相关资源
最近更新 更多