Commit 683b01807250481e0874c533b6018c81420e3ff5
- Date: Sat Jan 19 19:43:28 +0000 2008
- Committer: Benjamin Collins (ben.collins@acm.org)
- Author: Benjamin Collins (ben.collins@l-3com.com)
- Commit SHA1: 683b01807250481e0874c533b6018c81420e3ff5
- Tree SHA1: becd15e5c1d1edacbc3a0e20998675ebac20b455
[WiP] readjusting the itoc function. Needs to be itoa really, and I
should check my stdlib headers to see if it's already there.
Commit diff
| |   |
| 4 | 4 | //////////////////////////////////////////////////////////////// |
| 5 | 5 | #ifndef _SHA_BITS_HPP_ |
| 6 | 6 | #define _SHA_BITS_HPP_ |
| 7 | | |
| 8 | 7 | #include <algorithm> |
| 9 | 8 | #include <boost/dynamic_bitset.hpp> |
| 10 | 9 | |
| … | … | |
| 21 | 21 | } |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | | inline const char itoc(unsigned int n, int radix) |
| 24 | inline const char* itoc(unsigned int n, int radix) |
| 25 | 25 | { |
| 26 | char digits[8] = {0}; |
| 27 | unsigned int tmp = 0; |
| 28 | int i =0; |
| 29 | |
| 30 | while (n >= radix) |
| 31 | { |
| 32 | tmp = n / radix; |
| 33 | n -= tmp; |
| 34 | |
| 35 | } |
| 26 | 36 | unsigned char n0 = n % radix; |
| 27 | 37 | unsigned char n1 = n / radix; |
| 28 | 38 | |
| 29 | | return (n1 << 4 | n0); |
| 39 | |
| 30 | 40 | } |
| 31 | 41 | } |
| 32 | 42 | |
| toggle raw diff |
--- a/bits.hpp
+++ b/bits.hpp
@@ -4,7 +4,6 @@
////////////////////////////////////////////////////////////////
#ifndef _SHA_BITS_HPP_
#define _SHA_BITS_HPP_
-
#include <algorithm>
#include <boost/dynamic_bitset.hpp>
@@ -22,12 +21,22 @@ namespace bits {
}
}
- inline const char itoc(unsigned int n, int radix)
+ inline const char* itoc(unsigned int n, int radix)
{
+ char digits[8] = {0};
+ unsigned int tmp = 0;
+ int i =0;
+
+ while (n >= radix)
+ {
+ tmp = n / radix;
+ n -= tmp;
+
+ }
unsigned char n0 = n % radix;
unsigned char n1 = n / radix;
- return (n1 << 4 | n0);
+
}
}
|
| |   |
| 47 | 47 | |
| 48 | 48 | for (int i=0; i<num_bytes; ++i) |
| 49 | 49 | { |
| 50 | | result.push_back(bits::itoc(((data>>(i*8)) & lsb_mask).to_ulong(), base)); |
| 50 | char c = bits::itoc(((data>>(i*8)) & lsb_mask).to_ulong(), base); |
| 51 | result.push_back(c); |
| 51 | 52 | } |
| 52 | 53 | |
| 53 | 54 | bits::reverse_bytes(result.begin(), result.end()); |
| toggle raw diff |
--- a/sha.hpp
+++ b/sha.hpp
@@ -47,7 +47,8 @@ std::string to_string(std::bitset<Bits>& data, int base)
for (int i=0; i<num_bytes; ++i)
{
- result.push_back(bits::itoc(((data>>(i*8)) & lsb_mask).to_ulong(), base));
+ char c = bits::itoc(((data>>(i*8)) & lsb_mask).to_ulong(), base);
+ result.push_back(c);
}
bits::reverse_bytes(result.begin(), result.end()); |
| |   |
| 3 | 3 | // \date January 2008 |
| 4 | 4 | //////////////////////////////////////////////////////////////// |
| 5 | 5 | #include "sha.hpp" |
| 6 | #include <bitset> |
| 6 | 7 | #include <iostream> |
| 7 | 8 | #include <string> |
| 8 | 9 | |
| 9 | 10 | int main() |
| 10 | 11 | { |
| 11 | | sha1_t hash0(std::string("hello")); |
| 12 | std::bitset<8> testbs(std::string("10101010")); |
| 12 | 13 | |
| 13 | | std::cout << hash0 << std::endl; |
| 14 | std::cout << to_string(testbs, 16) << std::endl; |
| 14 | 15 | } |
| toggle raw diff |
--- a/test0.cpp
+++ b/test0.cpp
@@ -3,12 +3,13 @@
// \date January 2008
////////////////////////////////////////////////////////////////
#include "sha.hpp"
+#include <bitset>
#include <iostream>
#include <string>
int main()
{
- sha1_t hash0(std::string("hello"));
+ std::bitset<8> testbs(std::string("10101010"));
- std::cout << hash0 << std::endl;
+ std::cout << to_string(testbs, 16) << std::endl;
} |