|
|
@@ -47,17 +47,16 @@ namespace pkpy{
|
|
|
vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
|
|
|
const C99Struct& self = _CAST(C99Struct&, args[0]);
|
|
|
SStream ss;
|
|
|
- ss << "0x";
|
|
|
for(int i=0; i<self.size; i++) ss.write_hex((unsigned char)self.p[i]);
|
|
|
return VAR(ss.str());
|
|
|
});
|
|
|
|
|
|
// @staticmethod
|
|
|
- vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
|
|
|
+ vm->bind_func<1>(type, "fromhex", [](VM* vm, ArgsView args){
|
|
|
const Str& s = CAST(Str&, args[0]);
|
|
|
- if(s.size<4 || s[0]!='0' || s[1]!='x' || s.size%2!=0) vm->ValueError("invalid hex string");
|
|
|
- C99Struct buffer(s.size/2-1, false);
|
|
|
- for(int i=2; i<s.size; i+=2){
|
|
|
+ if(s.size<2 || s.size%2!=0) vm->ValueError("invalid hex string");
|
|
|
+ C99Struct buffer(s.size/2, false);
|
|
|
+ for(int i=0; i<s.size; i+=2){
|
|
|
char c = 0;
|
|
|
if(s[i]>='0' && s[i]<='9') c += s[i]-'0';
|
|
|
else if(s[i]>='A' && s[i]<='F') c += s[i]-'A'+10;
|
|
|
@@ -66,7 +65,7 @@ namespace pkpy{
|
|
|
if(s[i+1]>='0' && s[i+1]<='9') c += s[i+1]-'0';
|
|
|
else if(s[i+1]>='A' && s[i+1]<='F') c += s[i+1]-'A'+10;
|
|
|
else vm->ValueError(fmt("invalid hex char: '", s[i+1], "'"));
|
|
|
- buffer.p[i/2-1] = c;
|
|
|
+ buffer.p[i/2] = c;
|
|
|
}
|
|
|
return VAR_T(C99Struct, std::move(buffer));
|
|
|
}, {}, BindType::STATICMETHOD);
|