ASCII to BCD via bit packing / shifts / masks

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 3162 Views - Last Post: 24 September 2015 - 02:58 PM Rate Topic: -----

#1 soundchaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-September 15

ASCII to BCD via bit packing / shifts / masks

Posted 18 September 2015 - 02:52 PM

I need a starting place to begin a homework assignment. This class is not structured and is a learn on your own style course... And I cannot find any sources online to explain what the instructions are asking.

The Assignment instructions is to implement a BCD character encoding of an IBM 704: BCD table here via bit packing masking and shifting
  • Include methods for encoding and decoding individual BCD characters
  • Include methods for converting to and from BCD and ascii characters
  • Add methods for storing and retrieving 5 BCD characters in each unsigned int in that array. The first character should be in bits 0-5, the second, in bits 6-11, the third in bits 12-17, the fourth, in bits 18-23, and the 6th in bits 24-29. Bits 30 and 31 are unused and should be set to zero. Your Bcd class should allow allocation of a dynamic array of unsigned integers
  • Add methods to pack and unpack entire strings of ascii characters into array of integers of length n with 5 BCD characters per 32bit integer.


Before diving into this assignment, the class has only had experience with Java and C#. I am not familiar with the idea of bit packing or encoding at all, and only have the vaguest idea of how to do basic bit shifting, eg: binary value 010 << 1 = 100

Here is the only example we were given to dissect and work with:
#include <iostream>
using namespace std;
typedef struct RGB {  
	unsigned int s, b, g, r;  
}RGB;  
unsigned int packRGB(const RGB *rgb) {  
	return rgb->s << 24 | rgb->b << 16 | rgb->g << 8 | rgb->r;  
}  
RGB unpackRGB(unsigned int rgb) {  
	RGB unpacked = {rgb >> 24 & 0XFF, rgb >> 16 & 0XFF, rgb >> 8 & 0XFF, rgb & 0XFF};  
	return unpacked;  
	}  


I just need to know what to even start doing, and possibly a layman's explanation of what that example is doing. My biggest problem, is I don't know how to even begin understanding the supplied BCD table.

Is This A Good Question/Topic? 0
  • +

Replies To: ASCII to BCD via bit packing / shifts / masks

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 18 September 2015 - 09:27 PM

View Postsoundchaos, on 18 September 2015 - 05:52 PM, said:

My biggest problem, is I don't know how to even begin understanding the supplied BCD table.

Yeah. The editor of that article really did you a disservice by showing the values in octal for the row and column headers rather than binary, and then using that grid pattern.

Here's the information in that table formatted another way. Hopefully you'll start to see the pattern with this layout:
000000  0    010000  +    100000  -    110000  [space]
000001  1    010001  A    100001  J    110001  /
000010  2    010010  B    100010  K    110010  S
000011  3    010011  C    100011  L    110011  T
000100  4    010100  D    100100  M    110100  U
000101  5    010101  E    100101  N    110101  V
000110  6    010110  F    100110  O    110110  W
000111  7    010111  G    100111  P    110111  X
001000  8    011000  H    101000  Q    111000  Y
001001  9    011001  I    101001  R    111001  Z
001010       011010       101010       111010
001011  =    011011  .    101011  $    111011  ,
001100  -    011100  )    101100  *    111100  (
001101       011101       101101       111101
001110       011110       101110       111110
001111       011111       101111       111111


Was This Post Helpful? 1
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 18 September 2015 - 09:43 PM

Staring at the punch card that lead to BCD may also help if you are visual learner.
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: ASCII to BCD via bit packing / shifts / masks

Posted 18 September 2015 - 10:23 PM

have a look at
http://www.ee.ic.ac....%20to%20BCD.pdf
Was This Post Helpful? 0
  • +
  • -

#5 soundchaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-September 15

Re: ASCII to BCD via bit packing / shifts / masks

Posted 18 September 2015 - 11:30 PM

View PostSkydiver, on 18 September 2015 - 09:27 PM, said:

View Postsoundchaos, on 18 September 2015 - 05:52 PM, said:

My biggest problem, is I don't know how to even begin understanding the supplied BCD table.

Yeah. The editor of that article really did you a disservice by showing the values in octal for the row and column headers rather than binary, and then using that grid pattern.

Here's the information in that table formatted another way. Hopefully you'll start to see the pattern with this layout:
000000  0    010000  +    100000  -    110000  [space]
000001  1    010001  A    100001  J    110001  /
000010  2    010010  B    100010  K    110010  S
000011  3    010011  C    100011  L    110011  T
000100  4    010100  D    100100  M    110100  U
000101  5    010101  E    100101  N    110101  V
000110  6    010110  F    100110  O    110110  W
000111  7    010111  G    100111  P    110111  X
001000  8    011000  H    101000  Q    111000  Y
001001  9    011001  I    101001  R    111001  Z
001010       011010       101010       111010
001011  =    011011  .    101011  $    111011  ,
001100  -    011100  )    101100  *    111100  (
001101       011101       101101       111101
001110       011110       101110       111110
001111       011111       101111       111111



This helps a ton so far! Now, what would I need to begin to do, If I were to take an ascii string into a method, and convert it to this BCD? This is my rough pseudocode idea so far..

int convertToBCD(string asciiString)
{
toUpper(asciiString);

*take ascii value of each string char and convert to value of equivalent bcd*
*if ascii char is not part of BCD, replace it with ??*

return bcd

}


Given one of my end goals is to take a full novel, import it into a single string, compress it into BCD, output the BCD compressed file, take that file, and have another method to convert it back to ascii, I'd also want to take one of those blank bcd characters and make it into a carriage return.
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 12:16 AM

this gives an example of converting decimal 162 to BCD
http://www.eng.utah....Conversion.html

with an algorithm which is fairly simple to implement in C
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 08:01 AM

View Postsoundchaos, on 19 September 2015 - 02:30 AM, said:

Given one of my end goals is to take a full novel, import it into a single string, compress it into BCD, output the BCD compressed file, take that file, and have another method to convert it back to ascii, I'd also want to take one of those blank bcd characters and make it into a carriage return.


If if look closely, you'll notice that CR (0x13) and LF (0x0A) conveniently are already on the blank spots.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3778
  • View blog
  • Posts: 13,688
  • Joined: 08-August 08

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 09:32 AM

CR is 0xD, or decimal 13. ;)
Was This Post Helpful? 2
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 11:05 AM

Sorry, my fault. I was intending to type in the decimal and hex values, but the baby started crying.
Was This Post Helpful? 0
  • +
  • -

#10 soundchaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-September 15

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 11:09 AM

View Posthorace, on 19 September 2015 - 12:16 AM, said:

this gives an example of converting decimal 162 to BCD
http://www.eng.utah....Conversion.html

with an algorithm which is fairly simple to implement in C


Been trying to study whats going on here, but the Verilog goes over my head. I still cant figure out how to do the conversion, or how bit shifting works with this whole program. There is still some crucial step that i'm missing that's preventing me from even knowing what to research.

Do I need to have a separate conversion and encoding method, or should both be possible with one method?

If I am to do a conversion to BCD method, do I need to use an array that contains the values in order of all the bcd chars? And if so, whats an example of how I would implement it into an encoding/decoding?

Sorry for being such a newbie at this, I wish I could get help with the teacher, but he just keeps saying everything I need is in the first example he provided, and I just don't see any relationship between that and what I'm supposed to do.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 11:16 AM

That link presents how to do binary to BCD for numbers. It's the Double Dabble algorithm. Eyeballing that algorithm there, it doesn't cover ASCII letters to BCD characters.
Was This Post Helpful? 0
  • +
  • -

#12 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,832
  • Joined: 25-October 06

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 12:41 PM

View Postsoundchaos, on 19 September 2015 - 07:09 PM, said:

Been trying to study whats going on here, but the Verilog goes over my head. I still cant figure out how to do the conversion, or how bit shifting works with this whole program. There is still some crucial step that i'm missing that's preventing me from even knowing what to research.

Verilog is a hardware description language - not used it myself - I used VHDL
however, I thought you were implementing the BCD converter in C or Java

the reference in post #6 shows the pseudo-code for the double dabble algorithm
http://www.eng.utah....Conversion.html

although the sample code is Verilog the pseudo-code can be implemented in C, Java, C++, etc using loops, bitwise shifts (>> <<) and AND (&) operations

I have an implementation with function prototype
// decimal to BCD using double dabble algorithm from
//  http://www.eng.utah.edu/~nmcdonal/Tutorials/BCDTutorial/BCDConversion.html
//   convert parameter data (8 bit ) to BCD[]
//  most sigmificant in bcd[2] least in bcd[0], e.g. 162 = bcd[2]=1 bcd[1]=6 bcd[0]=2
void bcd1(int data, int bcd[])


it would be called so
    int i, j, data=162,decimal=0, bcd[3]={0};
    printf("data %d in hex %x\n", data, data);
    bcd1(data, bcd);
    printf("\nBCD1 result ");
     for (j=2; j>=0; j--)
       printf("%d", bcd[j]);


you can use a similar technique or some variation of your own

This post has been edited by horace: 19 September 2015 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 02:12 PM

The operators that modify the bits of a value are called bitwise operators.

Bitwise operators
Was This Post Helpful? 0
  • +
  • -

#14 soundchaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-September 15

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 02:17 PM

View Posthorace, on 19 September 2015 - 12:41 PM, said:

View Postsoundchaos, on 19 September 2015 - 07:09 PM, said:

Been trying to study whats going on here, but the Verilog goes over my head. I still cant figure out how to do the conversion, or how bit shifting works with this whole program. There is still some crucial step that i'm missing that's preventing me from even knowing what to research.

Verilog is a hardware description language - not used it myself - I used VHDL
however, I thought you were implementing the BCD converter in C or Java

the reference in post #6 shows the pseudo-code for the double dabble algorithm
http://www.eng.utah....Conversion.html

although the sample code is Verilog the pseudo-code can be implemented in C, Java, C++, etc using loops, bitwise shifts (>> <<) and AND (&) operations

I have an implementation with function prototype
// decimal to BCD using double dabble algorithm from
//  http://www.eng.utah.edu/~nmcdonal/Tutorials/BCDTutorial/BCDConversion.html
//   convert parameter data (8 bit ) to BCD[]
//  most sigmificant in bcd[2] least in bcd[0], e.g. 162 = bcd[2]=1 bcd[1]=6 bcd[0]=2
void bcd1(int data, int bcd[])


it would be called so
    int i, j, data=162,decimal=0, bcd[3]={0};
    printf("data %d in hex %x\n", data, data);
    bcd1(data, bcd);
    printf("\nBCD1 result ");
     for (j=2; j>=0; j--)
       printf("%d", bcd[j]);


you can use a similar technique or some variation of your own

I am trying to do this all in C++, but I'm not sure I understand what the intent is with your function prototype
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6117
  • View blog
  • Posts: 21,060
  • Joined: 05-May 12

Re: ASCII to BCD via bit packing / shifts / masks

Posted 19 September 2015 - 04:23 PM

The prototype was just to show how the function is being called. You pass in a value and an integer array, and on return, the integer array would be filled with the decimal digits of the value. That would work equally well with C or C++. Modern C++ would likely prefer a prototype that looks like: std::vector<int> bcd1(int data).
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3