Rev 1657 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1647 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "common.h" |
||
| 22 | |||
| 23 | namespace atom { |
||
| 24 | namespace common { |
||
| 25 | |||
| 26 | std::string PadNumber(unsigned int number, unsigned int length) |
||
| 27 | { |
||
| 28 | std::string padded_string = boost::lexical_cast<std::string>(number); |
||
| 29 | |||
| 30 | while (padded_string.length() < length) |
||
| 31 | { |
||
| 32 | padded_string = "0" + padded_string; |
||
| 33 | } |
||
| 34 | |||
| 35 | return padded_string; |
||
| 36 | } |
||
| 37 | |||
| 38 | std::string ToHex(unsigned int value) |
||
| 39 | { |
||
| 40 | char hex_string[11]; |
||
| 41 | |||
| 42 | snprintf(hex_string, sizeof(hex_string), "0x%08X", value); |
||
| 43 | |||
| 44 | return std::string(hex_string); |
||
| 45 | |||
| 46 | } |
||
| 47 | |||
| 48 | unsigned int FromHex(std::string hex_chars) |
||
| 49 | { |
||
| 50 | int value = 0; |
||
| 51 | int ascii_value; |
||
| 52 | |||
| 53 | for (int n = hex_chars.length() - 1; n >= 0; n--) |
||
| 54 | { |
||
| 55 | ascii_value = (int)hex_chars.data()[n]; |
||
| 56 | |||
| 57 | if (48 <= ascii_value && ascii_value <= 57) |
||
| 58 | { |
||
| 59 | value |= (ascii_value - 48) << n; |
||
| 60 | } |
||
| 61 | else if (65 <= ascii_value && ascii_value <= 70) |
||
| 62 | { |
||
| 63 | value |= (ascii_value - 65) << n; |
||
| 64 | } |
||
| 65 | else |
||
| 66 | { |
||
| 67 | 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())); |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return value; |
||
| 72 | } |
||
| 73 | |||
| 74 | }; // namespace type |
||
| 75 | }; // namespace atom |