Fixed overflow bug

cli
E. Almqvist 2 years ago
parent e1c3d15158
commit 17af660117
  1. 3
      src/main.rs
  2. 9
      src/permutations.rs
  3. 24
      src/util.rs

@ -12,5 +12,6 @@ fn main() {
// scanner::start_scan(100); // scanner::start_scan(100);
// permutations::ipv4(None); // permutations::ipv4(None);
let ip = permutations::IPv4::new(0); let ip = permutations::IPv4::new(256);
println!("{:?}", ip);
} }

@ -11,18 +11,19 @@ let i = 0 .. u32:max_value()
# This is waaaay better than a stupid loop # This is waaaay better than a stupid loop
*/ */
#[derive(Debug)]
pub struct IPv4 { pub struct IPv4 {
pub id: u32, pub id: u64,
pub ip: Vec<u8> pub ip: Vec<u16>
} }
impl IPv4 { impl IPv4 {
pub fn new(id: u32) -> Self { pub fn new(id: u64) -> Self {
let mut base = Convert::new(10, 256); let mut base = Convert::new(10, 256);
let id_vec = util::number_to_vec(id); // push all digits into a vec let id_vec = util::number_to_vec(id); // push all digits into a vec
println!("########## {:?}", id_vec); println!("########## {:?}", id_vec);
let ip = base.convert::<u8, u8>(&id_vec); let ip = base.convert::<u8, u16>(&id_vec);
Self { id, ip } Self { id, ip }
} }

@ -10,13 +10,18 @@ uint ulong_len(ulong n) { // get the digit length of a number
} }
*/ */
fn digit(num: u32, idx: u32) -> u8 { fn digit(num: u64, idx: u32) -> u8 {
// ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx) // ((num % (10**(idx+1))) - (num % (10**idx)))/(10**idx)
(((num % (10_u32.pow(idx+1))) - (num % (10_u32.pow(idx))))/(10_u32.pow(idx))) as u8 (((num % (10_u64.pow(idx+1))) - (num % (10_u64.pow(idx))))/(10_u64.pow(idx))) as u8
} }
fn numlen(num: u32) -> u8 { fn numlen(mut num: u64) -> u8 {
let len: u8 = 0; let mut len: u8 = 0;
if num == 0 {
return 1;
}
while num != 0 { while num != 0 {
num = num / 10; num = num / 10;
len += 1; len += 1;
@ -24,14 +29,19 @@ fn numlen(num: u32) -> u8 {
len len
} }
pub fn number_to_vec(num: u32) -> Vec<u8> { pub fn number_to_vec(num: u64) -> Vec<u8> {
let out: Vec<u8> = Vec::new(); let mut out: Vec<u8> = Vec::new();
let len = numlen(num); let len = numlen(num);
println!("len={len}");
for idx in 0..len { for idx in 0..len {
out.push(digit(num, idx as u32)); let digit = digit(num, idx as u32);
println!("\t{:?}", digit);
out.push(digit);
} }
println!("{:?}", out);
out out
} }

Loading…
Cancel
Save