Link to home
Start Free TrialLog in
Avatar of fareezaa
fareezaa

asked on

Convert input string in Little-endian

Hello,
  I need to convert a string value to 4-byte BCD representation, in little-endian format.  How do I do that?
Avatar of zebada
zebada

Not sure how you mean to encode BCD as "little endian"
My guess is you want to see the string 12345678
stored as HEX: 87 65 43 21

This code should do that for you

#include <stdio.h>
#include <string.h>

typedef unsigned char BYTE;

int main(int argc, char *argv[])
{
  char *c,*s = "45678";
  BYTE *b,bcd[4];
  int i;

  for ( b=bcd,i=0 ; i<4 ; i++ )
    *b++ = 0;
  b = bcd;
  for ( c=s+strlen(s)-1 ; s<=c ; c-=2 ) {
    *b = (*c-'0') << 4;
    if ( s<c )
      *b |= (*(c-1)-'0');
    b++;
  }

  for ( i=0 ; i<4 ; i++ )
    printf("%2.2x,",bcd[i]);
}
>> My guess is you want to see the string 12345678
>> stored as HEX: 87 65 43 21
That is big endian.
87 65 43 21

is little endian.

You can just reverse the final product.
neitod, huh?
Isn't little endian the LSB first?
Opps, I didn't reverse it!

Little endian means that the little part, the least significant part, appears at the end, at the highest address.  So a number like

12345678

would be stored as 4 bytes from low to high addresses that contain

0x12 0x34 0x56 0x78

bummer!!!!

I guess that's why I always refer to "MSB first" or "LSB first". Never knew what "endian" meant - from your explanation I guess it means "end".
Avatar of fareezaa

ASKER

ok.
but i need to prompt the user for the value.  From the example you showed, both pointers value has been been initialized with values.  Using that it works, but when i modify the code to get user input,  It gives me error.  This is the snippet of the code:
--
char *c, *s;
BYTE *b,bcd[4];
int i=0;

printf("\n\nPlease enter your PIN to log in or 'q' to quit:\n");
scanf("%s", &c);
printf("%s",&c);
--

how can i copy these value to s as well? or get this code to be working?
and i forgot to include that the char *c, *s needs to be as unsigned char s[].
You are using scanf() incorrecly.  When you read a string wiht scanf() you need to pass a pointer to an array of characters that will receive the result. Like

char c[100]; // Array to store a string of up to 99 characters.

scanf("%s", c);


However, it is a bad idea to be using C I/O in C++ programs.  As you can tell, it is easy to use it correctly adn make mistakes that cause crashes or other problems.

Instead use C++ i/O like


char c[100];

cin >> c;


Or better yet, use string objects like

string c;

cin >> string.
Hello,
 when i do it that way, i still get errors. the errors are:
1. error C2440: '=' : cannot convert from 'char *' to 'char [30]'.  There are no conversions to array types, although there are conversions to references or pointers to arrays
2. error C2106: '-=' : left operand must be l-value.

the code:

int main(int argc, char *argv[])
{
char *s ;
BYTE *b,bcd[4];
int i=0;
char c[30];

printf("\n\nPlease enter your PIN to log in or 'q' to quit:\n");
cin>>c;
s = c;  //i need to copy value of c to s

for ( b=bcd,i=0 ; i<4 ; i++ )
*b++ = 0;
b = bcd;
for ( c=s+strlen(s)-1 ; s<=c ; c-=2 ) {                        <- error 1 and 2
     *b = (*c-'0') << 4;
      if ( s<c )
     *b |= (*(c-1)-'0');
      b++;
}

for ( i=0 ; i<4 ; i++ )
printf("%2.2x ",bcd[i]);
}
Avatar of ozo
for ( s=c+strlen(c)-1 ; c<=s ; s-=2 ) {  
    *b = (*s-'0') << 4;
     if ( c<s )
    *b |= (*(s-1)-'0');
     b++;
}
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
how to i print out the return value of Hex2Bin(uByt h) function?

i did this:
char c[30];
     
printf("\n\nPlease enter your PIN to log in or 'q' to quit:\n");
cin >> c;
     
printf("\nthe result is: \"%s\"\n", c);
s = c;
cout<<"\ns value: "<<s;

BYTE x = Hex2Bin(*c);
cout<<"\nx value: "<<x;
--
and i get weird output..a symbol of a smiling face.

>> and i get weird output..a symbol of a smiling face.
That would be very likely.  Its a byte containing a packed BCD value.  its not necessarily going to be a valid ASCII character.

You can convert it to a number (int) then print the int.

cout << (int) Hex2Bin(*c);
hello,
when i convert [the cout << (int) Hex2Bin(*c)],the answer is 1.  that is not what i am looking for.  
When the user input :12345678,
the answer shud be {{0x87}, {0x65}, {0x43}, {0x21}}; but i don't know how to get this.
I've tried zebada's way but it's the other way around (87 65 43 21) and it is not working.
:(
hex2Bin is not meant to do that conversion.  Read the comments about it It converts a single ASCII digit/letter to a  byte that contains the binary value represened by the ASCII value.  I.e. it converts the digit "1" to binary 1.  It converts "A" to binary 10 (0xA)  it converts "b" to 11 and so on.

The procedure is used by QMthPckASC()  That procedure  packs one string of ASCII digits/letters to a string of binary BCD bytes.

Read the commens on it.  it explains what it does and what each parameter is.
when i use that way, it will return me 0x87 0x65 0x43 0x21.  the expected result is 0x78 0x56 0x34 0x12.   so i change some of the code and it works. But what if the input is odd, say 12345.  Should it be
a-(0x54 0x32 0x01)
 or
b- (0x05 0x43 0x21)?
>>  it will return me 0x87 0x65 0x43 0x21
If you feed it a string that is (from low to high address) "12345678" it will return to you a string that is (from low to high)
0x12, 0x34, ix56, 0x78.

>> Should it be
What do you want?  It should be what you need according to your needs.
THANKS.  i think it shud be b+, not b
You could have rounded it up to an A.  :-)