Board index » cppbuilder » little endian format...

little endian format...


2006-06-29 12:38:29 AM
cppbuilder53
Hi all, just a quicky.
I'm working with some hardware and it is expecting me to pass a user pin
number to it in little endian format. The hardware api function expects an
unsigned char [] array in little endian format however i have the pin in a
string. If there any function for performing this convertion or should I
just get on code it myself?
Thanks in advance...
 
 

Re:little endian format...

Mike Collins wrote:
Quote
Hi all, just a quicky.

I'm working with some hardware and it is expecting me to pass a user pin
number to it in little endian format. The hardware api function expects an
unsigned char [] array in little endian format however i have the pin in a
string. If there any function for performing this convertion or should I
just get on code it myself?

Thanks in advance...


I think you don't have to worry about the endianness when working with
bytes (unsigned char). Moreover the x86 platform is little endian.
Muzaffar
 

Re:little endian format...

Mike Collins wrote:
Quote
I'm working with some hardware and it is expecting me to pass a user pin
number to it in little endian format. The hardware api function expects an
unsigned char [] array in little endian format however i have the pin in a
string. If there any function for performing this convertion or should I
just get on code it myself?
So, if the pin is
char pin[] = "123456";
Your HW wants to see the 6 first?
How about strrev() ?
 

{smallsort}

Re:little endian format...

Ok, this is going to sound crazy but (and this is the example give)
if the user Pin were "12345678" (and it can only be numeric and 8 char's
long), the example given is:
unsigned char cUserPin[] = { {0x78}, {0x56}, {0x34}, {0x12} };
- which really is assigning the hex value - this seems to me like total
madness but that's what I'm working with. However, just to add to the
madness, my pin variable is an unsigned long,
"Bob Gonder" < XXXX@XXXXX.COM >wrote in message
Quote
Mike Collins wrote:

 

Re:little endian format...

thanks for the reply, please see my reply to Bob Gonder which clarifies it a
bit
"Muzaffar Mahkamov" < XXXX@XXXXX.COM >wrote in message
Quote
Mike Collins wrote:
>Hi all, just a quicky.
>
 

Re:little endian format...

I've got something working but it is a botch. I had orginally thought that
I could resolve this with some bitwise manipulation but then problem was
componded by the fact that it is byte.
unsigned char cUPin[4];
TBcd bcdPin = IntegerToBcd(12345678);//iSerialNum[1]);
cUPin[0] = bcdPin.Fraction[3];
cUPin[1] = bcdPin.Fraction[2];
cUPin[2] = bcdPin.Fraction[1];
cUPin[3] = bcdPin.Fraction[0];
it's dirty but it works based on the fact that I know that the pin will onlt
be 8 chars long...
"Mike Collins" < XXXX@XXXXX.COM >wrote in message
Quote
Hi all, just a quicky.

I'm working with some hardware and it is expecting me to pass a user pin
number to it in little endian format. The hardware api function expects
an unsigned char [] array in little endian format however i have the pin
in a string. If there any function for performing this convertion or
should I just get on code it myself?

Thanks in advance...

 

Re:little endian format...

Mike Collins wrote:
Quote
I've got something working but it is a botch. I had orginally thought that
I could resolve this with some bitwise manipulation but then problem was
componded by the fact that it is byte.

unsigned char cUPin[4];
TBcd bcdPin = IntegerToBcd(12345678);//iSerialNum[1]);
cUPin[0] = bcdPin.Fraction[3];
cUPin[1] = bcdPin.Fraction[2];
cUPin[2] = bcdPin.Fraction[1];
cUPin[3] = bcdPin.Fraction[0];

it's dirty but it works based on the fact that I know that the pin will onlt
be 8 chars long...
From your other message it's easy to see that it wants packed bcd, and
right-to-left.
You could rely on always having TBcd avialable, or you could do it
yourself, and have any length.
First, get an ascii representation with itoa or sprintf.
If you use itoa then you need to worry about odd digits.
char value[64];
// must be even number of digits //
#define digits 8
Assert( digits<63 );
Assert( (digits & 1) == 0 );
sprintf( value, "%*.*i", digits, digits, pin );
// must be even number of digits //
If you use itoa then you need to worry about odd digits.
Then output the little endian packed bcd
char*p = &value[digits-1];
do{
send_to_hw( (((*p[-1])&0x0f)<<4) | ((*p)&0x0f) );
p -= 2;
}while( p>value );
- or -
char*p = &value[digits-1];
char* o = cUPin;
do{
*o = (((*p[-1])&0x0f)<<4) | ((*p)&0x0f);
++o;
p -= 2;
}while( p>value );
 

Re:little endian format...

Miguel Gimenez < XXXX@XXXXX.COM >writes:
Quote
>I'm working with some hardware and it is expecting me to pass a
>user pin number to it in little endian format. The hardware api
>function expects an unsigned char [] array in little endian format
>however i have the pin in a string. If there any function for
>performing this convertion or should I just get on code it myself?
>Thanks in advance...

Check atoi()
Only to avoid it. If at all, use one of the stro*(), since they offer
proper error handling.
 

Re:little endian format...

XXXX@XXXXX.COM (Thomas Maeder [TeamB]) writes:
Quote
Miguel Gimenez < XXXX@XXXXX.COM >writes:

>>I'm working with some hardware and it is expecting me to pass a
>>user pin number to it in little endian format. The hardware api
>>function expects an unsigned char [] array in little endian format
>>however i have the pin in a string. If there any function for
>>performing this convertion or should I just get on code it myself?
>>Thanks in advance...
>
>Check atoi()

Only to avoid it. If at all, use one of the stro*(), since they offer
proper error handling.
In case of confusion, it's strto* (strtol, strtoul, strtod, etc.)
--
Chris (TeamB);
 

Re:little endian format...

"Mike Collins" < XXXX@XXXXX.COM >escreveu na mensagem
Quote
Ok, this is going to sound crazy but (and this is the example give)

if the user Pin were "12345678" (and it can only be numeric and 8 char's
long), the example given is:

unsigned char cUserPin[] = { {0x78}, {0x56}, {0x34}, {0x12} };

I think its just a matter of converting the string to int64 and use only the
lower 32 bits..:
char cvt[16];
/* szPin is the string containing the pin in ascii format */
sprintf(cvt, "0x%s", szPin);
unsigned long ulPin = (unsigned long)atoi64(cvt);
char *pchUserPin = (char *)&ulPin;
So pchUserPin will point to your sequence in little endian format!
[]s
Fred
 

Re:little endian format...

Frederico pissarra wrote:
Quote
"Mike Collins" < XXXX@XXXXX.COM >escreveu na mensagem
news:44a310ff$ XXXX@XXXXX.COM ...
>Ok, this is going to sound crazy but (and this is the example give)
>
>if the user Pin were "12345678" (and it can only be numeric and 8
>char's long), the example given is:
>
>unsigned char cUserPin[] = { {0x78}, {0x56}, {0x34}, {0x12} };
>

I think its just a matter of converting the string to int64 and use
only the lower 32 bits..:

char cvt[16];

/* szPin is the string containing the pin in ascii format */
sprintf(cvt, "0x%s", szPin);
unsigned long ulPin = (unsigned long)atoi64(cvt);
char *pchUserPin = (char *)&ulPin;

So pchUserPin will point to your sequence in little endian format!

[]s
Fred
Why not just use atoi insead of atoi64? Why convet to 64bit if the
destination is 32bit? Unless I'm missing something here...
--
Stan
 

Re:little endian format...

"Stan R." <none@none>escreveu na mensagem
Quote
Frederico pissarra wrote:
>"Mike Collins" < XXXX@XXXXX.COM >escreveu na mensagem
>news:44a310ff$ XXXX@XXXXX.COM ...
>>Ok, this is going to sound crazy but (and this is the example give)
>>
>>if the user Pin were "12345678" (and it can only be numeric and 8
>>char's long), the example given is:
>>
>>unsigned char cUserPin[] = { {0x78}, {0x56}, {0x34}, {0x12} };
>>
>
>I think its just a matter of converting the string to int64 and use
>only the lower 32 bits..:
>
>char cvt[16];
>
>/* szPin is the string containing the pin in ascii format */
>sprintf(cvt, "0x%s", szPin);
>unsigned long ulPin = (unsigned long)atoi64(cvt);
>char *pchUserPin = (char *)&ulPin;
>
>So pchUserPin will point to your sequence in little endian format!
>
>[]s
>Fred

Why not just use atoi insead of atoi64? Why convet to 64bit if the
destination is 32bit? Unless I'm missing something here...

Stan, I'm assuming that the string could be greater than 8 digits, and, if
the string contains something greater than 2147483648 (unsigned), atoi()
will fail (0x80000000 (unsigned) = -2147483648 (signed)). So I think atoi64
is safer... of couse, if the string never gets more than 8 digits, it's safe
to use atoi()!
Ahhh... And I'm using atoi64() just because atoi() works with 31 bits + 1
(signal)... So, after atoi64() I cast to "unsigned long". This way I'm
dealing with "unsigned" convertions...
[]s
Fred
 

Re:little endian format...

Frederico pissarra wrote:
Quote
"Stan R." <none@none>escreveu na mensagem
news: XXXX@XXXXX.COM ...
>Frederico pissarra wrote:
>>"Mike Collins" < XXXX@XXXXX.COM >escreveu na mensagem
>>news:44a310ff$ XXXX@XXXXX.COM ...
>>>Ok, this is going to sound crazy but (and this is the example give)
>>>
>>>if the user Pin were "12345678" (and it can only be numeric and 8
>>>char's long), the example given is:
>>>
>>>unsigned char cUserPin[] = { {0x78}, {0x56}, {0x34}, {0x12} };
>>>
>>
>>I think its just a matter of converting the string to int64 and use
>>only the lower 32 bits..:
>>
>>char cvt[16];
>>
>>/* szPin is the string containing the pin in ascii format */
>>sprintf(cvt, "0x%s", szPin);
>>unsigned long ulPin = (unsigned long)atoi64(cvt);
>>char *pchUserPin = (char *)&ulPin;
>>
>>So pchUserPin will point to your sequence in little endian format!
>>
>>[]s
>>Fred
>
>Why not just use atoi insead of atoi64? Why convet to 64bit if the
>destination is 32bit? Unless I'm missing something here...
>

Stan, I'm assuming that the string could be greater than 8 digits,
and, if the string contains something greater than 2147483648
(unsigned), atoi() will fail (0x80000000 (unsigned) = -2147483648
(signed)). So I think atoi64 is safer... of couse, if the string
never gets more than 8 digits, it's safe to use atoi()!

Ahhh... And I'm using atoi64() just because atoi() works with 31 bits
+ 1 (signal)... So, after atoi64() I cast to "unsigned long". This
way I'm dealing with "unsigned" convertions...

[]s
Fred
I guess I read too quick. In that case the 64bit version is of course
better :-)
--
Stan