Subversion Repositories HomeAutomation

Rev

Rev 1642 | Rev 1914 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1642 Rev 1647
Line 51... Line 51...
51
        in >> std::hex >> out.value;
51
        in >> std::hex >> out.value;
52
        return in;
52
        return in;
53
    }
53
    }
54
};
54
};
55
 
55
 
56
inline unsigned int FromHex(std::string hex_chars)
56
std::string PadNumber(unsigned int number, unsigned int length);
57
{
-
 
58
    int value = 0;
-
 
59
    int ascii_value;
-
 
60
   
-
 
61
    for (int n = hex_chars.length() - 1; n >= 0; n--)
-
 
62
    {
-
 
63
        ascii_value = (int)hex_chars.data()[n];
-
 
64
   
-
 
65
        if (48 <= ascii_value && ascii_value <= 57)
-
 
66
        {
-
 
67
            value |= (ascii_value - 48) << n;
-
 
68
        }
-
 
69
        else if (65 <= ascii_value && ascii_value <= 70)
-
 
70
        {
-
 
71
            value |= (ascii_value - 65) << n;
-
 
72
        }
-
 
73
        else
-
 
74
        {
-
 
75
            throw std::runtime_error("This is not an hex string, hex_chars = " + hex_chars + ", ascii_value = " + boost::lexical_cast<std::string>(ascii_value) + ", n = " + boost::lexical_cast<std::string>(n) + ", length = " + boost::lexical_cast<std::string>(hex_chars.length()));
57
unsigned int FromHex(std::string hex_chars);
76
        }
-
 
77
    }
-
 
78
   
-
 
79
    return value;
-
 
80
}
-
 
81
 
-
 
82
inline std::string ToHex(unsigned int value)
58
std::string ToHex(unsigned int value);
83
{
-
 
84
    char hex_string[11];
-
 
85
   
-
 
86
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
-
 
87
   
-
 
88
    return std::string(hex_string);
-
 
89
   
-
 
90
}
-
 
91
   
59
   
92
}; // namespace type
60
}; // namespace type
93
}; // namespace atom
61
}; // namespace atom
94
 
62
 
95
#endif // TYPE_COMMON_H
63
#endif // TYPE_COMMON_H