BMW M3 Forum
BMW M3 Forum BMW M3 Gallery BMW M3 Reviews BMW M3 Social Groups BMW M3 Chat M3Forum Sponsors >>
Loading


Mobile M3forum
Go Back   BMW M3 Forum.com (E30 M3 | E36 M3 | E46 M3 | E92 M3 | F80/X) > BMW M3 Discussions > E46 M3 (2001-2006) > Coding and Tuning
Tire Rack Buy Winter Tires Now!
Not a member? Register Now!
Register Gallery All Albums Garage Search Today's Posts Mark Forums Read Calendar FAQ

Coding and Tuning Discuss all avenues of coding and tuning here!


Reply
 
Thread Tools Display Modes
Old Thu, Aug-14-2014, 06:21:31 AM   #11
NZ_M3
Registered User
 
Join Date: Apr 2005
Posts: 743
Reputation: 0 NZ_M3 will become famous soon enough





Default Re: VIN coding & decoding Perl script

I found this neat program on another forum ... seems to work great also (right down the bottom of the post)

http://www.forum.mbstuning.com/viewtopic.php?f=80&t=7

Change pdf to rar in attachment
Attached Files
File Type: pdf VIN To HEX.pdf (145.0 KB, 87 views)

Last edited by NZ_M3; Thu, Aug-14-2014 at 06:25:48 AM.
Jump to top NZ_M3 is offline   Reply With Quote
Sponsored Links
Register now and remove these ads
Old Wed, Sep-03-2014, 08:14:40 PM   #12
blarf
Registered User
 
Join Date: Sep 2014
Posts: 8
Reputation: 0 blarf is on a distinguished road

United States




Default Re: VIN coding & decoding Perl script

So, another way to think about the encoding is that you're writing out a series of 6-bit integers. For example in Ruby, the following would give you an array of 8-bit integers that you could call Array#pack on to turn into an array of bytes:

Code:
def encode_vin(vin)
  table = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

  max_length = (vin.length * (6/8.0)).ceil
  output = Array.new(max_length){0}

  targetBit = (output.length * 8) - 1
  (vin.length).times do |i|
    char = table.index(vin[vin.length-i-1])

    t_byte = targetBit / 8
    t_bit = 7 - (targetBit % 8)

    6.times do |j|
      if (char & (1 << j)) > 0
        output[t_byte] |= (1 << (t_bit))
      end
      t_bit += 1
      if (t_bit > 7)
        t_byte -= 1
        t_bit = 0
      end
    end

    targetBit -= 6
  end

  return output
end
And this would take an array of 8-bit integers and return a human readable string (if reading from a file or ROM dump you could use String#unpack to get such an array):

Code:
def decode_vin(array)
  vin = []
  table = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
  
  total_bits = array.length * 8
  (total_bits / 6).times {|i|
    n = (total_bits - (6*(i+1))) - 1;

    finish = n + 6
    start_byte = n / 8
    start_bit = (n % 8)
    finish_byte = finish / 8
    finish_bit = finish % 8

    if (start_byte == finish_byte)
      mask = (1 << (finish_bit - start_bit)) - 1
      vin.unshift(table[array[start_byte] & mask] || (array[start_byte] & mask))
    else
      start_bit += 1

      high_nibble = array[start_byte] & (0xff >> start_bit)
      low_nibble = array[finish_byte] & ((0xff << (7-finish_bit)) & 0xff)
      finish = (high_nibble << (6 - (8 - start_bit))) | (low_nibble >> (7 - finish_bit))

      vin.unshift(table[finish])
    end
  }

  return vin.join
end
The advantage here, of course, is that you avoid creating gigantic integers to set a few bits here and there.
Jump to top blarf is offline   Reply With Quote
Old Sat, Sep-06-2014, 05:24:33 AM   #13
p0lar
Naturally Agitated
 
p0lar's Avatar
 
Join Date: Sep 2005
Posts: 3,553
In the garage:
Reputation: 0 p0lar will become famous soon enough

United States




Default Re: VIN coding & decoding Perl script

Quote:
Originally Posted by blarf View Post
So, another way to think about the encoding is that you're writing out a series of 6-bit integers. For example in Ruby, the following would give you an array of 8-bit integers that you could call Array#pack on to turn into an array of bytes:

Code:
def encode_vin(vin)
  table = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

  max_length = (vin.length * (6/8.0)).ceil
  output = Array.new(max_length){0}

  targetBit = (output.length * 8) - 1
  (vin.length).times do |i|
    char = table.index(vin[vin.length-i-1])

    t_byte = targetBit / 8
    t_bit = 7 - (targetBit % 8)

    6.times do |j|
      if (char & (1 << j)) > 0
        output[t_byte] |= (1 << (t_bit))
      end
      t_bit += 1
      if (t_bit > 7)
        t_byte -= 1
        t_bit = 0
      end
    end

    targetBit -= 6
  end

  return output
end
And this would take an array of 8-bit integers and return a human readable string (if reading from a file or ROM dump you could use String#unpack to get such an array):

Code:
def decode_vin(array)
  vin = []
  table = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
  
  total_bits = array.length * 8
  (total_bits / 6).times {|i|
    n = (total_bits - (6*(i+1))) - 1;

    finish = n + 6
    start_byte = n / 8
    start_bit = (n % 8)
    finish_byte = finish / 8
    finish_bit = finish % 8

    if (start_byte == finish_byte)
      mask = (1 << (finish_bit - start_bit)) - 1
      vin.unshift(table[array[start_byte] & mask] || (array[start_byte] & mask))
    else
      start_bit += 1

      high_nibble = array[start_byte] & (0xff >> start_bit)
      low_nibble = array[finish_byte] & ((0xff << (7-finish_bit)) & 0xff)
      finish = (high_nibble << (6 - (8 - start_bit))) | (low_nibble >> (7 - finish_bit))

      vin.unshift(table[finish])
    end
  }

  return vin.join
end
The advantage here, of course, is that you avoid creating gigantic integers to set a few bits here and there.

Nice! Advantage is that Ruby is also native to OSX.
Jump to top p0lar is offline   Reply With Quote
Old Sat, Sep-13-2014, 12:09:44 AM   #14
blarf
Registered User
 
Join Date: Sep 2014
Posts: 8
Reputation: 0 blarf is on a distinguished road

United States




Default Re: VIN coding & decoding Perl script

Quote:
Originally Posted by p0lar View Post
Nice! Advantage is that Ruby is also native to OSX.
Well, Apple bundles Perl 5 too, but I don't know about the bigint module. Ruby's just what I'm used to, but that algorithm should be easy enough to transcribe to other languages (I've got a C++ implementation for my ds2 comms lib).

And, of course, you could probably optimize it a bit by replacing the loops with bit shifts...
Jump to top blarf is offline   Reply With Quote
Old Fri, Sep-09-2016, 08:33:01 AM   #15
behnamgh68
Registered User
 
Join Date: Sep 2016
Posts: 1
Reputation: 0 behnamgh68 is on a distinguished road

United States




Default Re: VIN coding & decoding Perl script

Quote:
Originally Posted by slim3 View Post
wbsbr93473pk03893
20 2d c2 db 24 31 07 0d 95 00 0c 82 43

wbsbl93486pn64443
20 2d c2 d5 24 31 08 19 95 c6 10 41 03
can you tell me the hex for this win
wbadt53451ce90553
Jump to top behnamgh68 is offline   Reply With Quote
Old Fri, Sep-09-2016, 11:27:41 AM   #16
p0lar
Naturally Agitated
 
p0lar's Avatar
 
Join Date: Sep 2005
Posts: 3,553
In the garage:
Reputation: 0 p0lar will become famous soon enough

United States




Default Re: VIN coding & decoding Perl script

Quote:
Originally Posted by behnamgh68 View Post
can you tell me the hex for this win
wbadt53451ce90553
wbadt53451ce90553 -> 20 2c a3 5d 14 31 05 04 c3 89 00 51 43
Jump to top p0lar is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT. The time now is 02:25:58 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
M3Forum.com and M3forum.net is in no way sponsored, endorsed or affiliated by or with BMW NA / BMW AG or any of it's subsidiaries or vendors.
BMW and M3 (E90 M3 | E92 M3 | E93 M3 | E46 M3 | E36 M3 | E30 M3) are registered trademarks of BMW AG.
M3Forum Terms of Service
Copyright 1999-2017 M3Forum.com
Discussing VIN coding & decoding Perl script in the Coding and Tuning Forum - Discuss all avenues of coding and tuning here! at BMW M3 Forum.com (E30 M3 | E36 M3 | E46 M3 | E92 M3 | F80/X)