Subversion Repositories HomeAutomation

Rev

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

Rev 1914 Rev 1939
Line 42... Line 42...
42
    char hex_string[11];
42
    char hex_string[11];
43
   
43
   
44
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
44
    snprintf(hex_string, sizeof(hex_string), "0x%08X", value);
45
   
45
   
46
    return std::string(hex_string);
46
    return std::string(hex_string);
47
   
-
 
48
}
47
}
49
 
48
 
50
std::string ToHex8bit(unsigned int value)
49
std::string ToHex8bit(unsigned int value)
51
{
50
{
52
    char hex_string[3];
51
    char hex_string[3];
-
 
52
   
53
    if (value > 255) {
53
    if (value > 255)
-
 
54
    {
54
      snprintf(hex_string, sizeof(hex_string), "--");
55
      snprintf(hex_string, sizeof(hex_string), "--");
-
 
56
    }
55
    } else {
57
    else
-
 
58
    {
56
      snprintf(hex_string, sizeof(hex_string), "%02x", value);
59
      snprintf(hex_string, sizeof(hex_string), "%02x", value);
57
    }
60
    }
58
    return std::string(hex_string);
-
 
59
   
61
   
-
 
62
    return std::string(hex_string);
60
}
63
}
61
 
64
 
62
 
65
 
63
std::string ToHex4bit(unsigned int value)
66
std::string ToHex4bit(unsigned int value)
64
{
67
{
65
    char hex_string[2];
68
    char hex_string[2];
-
 
69
 
66
    if (value > 15) {
70
    if (value > 15)
-
 
71
    {
67
      snprintf(hex_string, sizeof(hex_string), "-");
72
      snprintf(hex_string, sizeof(hex_string), "-");
68
    } else {
-
 
69
      snprintf(hex_string, sizeof(hex_string), "%01x", value);
-
 
70
    }
73
    }
-
 
74
    else
-
 
75
    {
-
 
76
      snprintf(hex_string, sizeof(hex_string), "%01x", value);
-
 
77
    }
71
   
78
   
72
    return std::string(hex_string);
79
    return std::string(hex_string);
-
 
80
}
-
 
81
 
-
 
82
unsigned int FromHex(std::string hex_chars)
-
 
83
{
-
 
84
    return static_cast<unsigned int>(strtoll(hex_chars.data(), NULL, 16));
-
 
85
}
-
 
86
 
-
 
87
std::string ToHexIfNumber(std::string data)
-
 
88
{
-
 
89
  bool is_number = true;
-
 
90
 
-
 
91
  if (data.length() == 0)
-
 
92
  {
-
 
93
    return data;
-
 
94
  }
-
 
95
 
-
 
96
  for (unsigned int n = 0; n < data.length(); n++)
-
 
97
  {
-
 
98
    if (!isdigit(data[n]))
-
 
99
    {
-
 
100
      is_number = false;
-
 
101
      break;
-
 
102
    }
-
 
103
  }
-
 
104
 
-
 
105
  if (is_number)
-
 
106
  {
-
 
107
    char *end = NULL;
-
 
108
    long value;
-
 
109
   
-
 
110
    value = strtol(data.c_str(), &end, 10);
73
   
111
   
-
 
112
    return ToHex(value);
74
}
113
  }
75
 
114
 
76
unsigned int FromHex(std::string hex_chars)
115
  return data;
77
{
-
 
78
    return static_cast<unsigned int>(strtoll(hex_chars.data(), NULL, 16));
-
 
79
}
116
}  
80
   
117
   
81
}; // namespace type
118
}; // namespace type
82
}; // namespace atom
119
}; // namespace atom