2016-04-11 14 views
0

Ich möchte einen vorzeichenlosen Wert in wchar_t Array kopieren. und mit diesemkonvertiert unsigned long in wchar_t

unsigned long lValue = <value>//value 

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

es funktioniert nicht für lange Werte größer als 2147483647 Was die Lösung?

+0

Was ist der Wert von 'wszFormat'? Ich würde "% lu" erwarten. – chux

+0

Danke .. das war der Fehler. Ich habe% d verwendet. – Sana

Antwort

0

Think wchar_t wird als 2 16bit int als UTF-16-Format gespeichert. Würde es nicht verwenden, außer du müsstest. Die folgende Struktur speichert jede Charta in einem einzelnen uint32_t (eine 32-Bit-Ganzzahl in C++). Aber wenn Sie einen unsigned long (64-Bit-Integer) Array benötigen, würde ich eines mit einem Vektor wie folgt erstellen:

std::vector<uint64_t> array; 

für 32-Bit-Anschluss oder weniger

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; 
}; 
+0

Während dieser Code die Frage beantworten kann, würde die Bereitstellung eines zusätzlichen Kontexts, der angibt, wie und/oder warum er das Problem löst, den langfristigen Wert der Antwort verbessern. – cpburnz