473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

6 byte integer to string

HI,
I face the task of converting a 6 byte integer (MSB first) to a
decimal string.
The platform I use supports only 4 byte basic data type. I have
written some algorithms that loops to many times :-(. It would be
great help if any of you can share a high performance algorithm to do
this.

Thanks
Sudi
Nov 14 '05 #1
9 3502
Hello,

"sudi" <su**********@vsnl.net> wrote in message
news:f6**************************@posting.google.c om...
HI,
I face the task of converting a 6 byte integer (MSB first) to a
decimal string.
The platform I use supports only 4 byte basic data type. I have
written some algorithms that loops to many times :-(. It would be
great help if any of you can share a high performance algorithm to do
this.

Thanks
Sudi


Can you give an example of what you want exactly?

Are you looking for something like IntegerToString() ?

--
Elias
Nov 14 '05 #2
"sudi" <su**********@vsnl.net> wrote in message
news:f6**************************@posting.google.c om...
I face the task of converting a 6 byte integer (MSB first) to a
decimal string.
The platform I use supports only 4 byte basic data type. I have
written some algorithms that loops to many times :-(. It would be
great help if any of you can share a high performance algorithm to do
this.


If the platform can do 32-bit division (32-bit dividend and divisor to
32-bit quotient and remainder) in hardware, then the quickest way is
probably to start by dividing the 48-bit number by 100000 in software. The
remainder from that division contains the least significant five digits, and
the quotient (which is guaranteed to fit in 32 bits) contains the most
significant digits. Then use the hardware divide to extract the digits from
each part (dividing by 10).

If there is no hardware divide, I don't know of any better way than the
obvious repeated division by 10. You could perhaps get some improvement by
reducing the size of the division as you go.

Alex
Nov 14 '05 #3
Alex Fraser wrote:
If the platform can do 32-bit division (32-bit dividend and divisor to
32-bit quotient and remainder) in hardware, then the quickest way is
probably to start by dividing the 48-bit number by 100000 in software. The
remainder from that division contains the least significant five digits, and
the quotient (which is guaranteed to fit in 32 bits) contains the most
significant digits. Then use the hardware divide to extract the digits from
each part (dividing by 10).

If there is no hardware divide, I don't know of any better way than the
obvious repeated division by 10.


I came up with a custom algorithm for doing unsigned 32-bit division of
x by 10 using only a couple shifts and x % 5 hardware multiplications.
I'm not sure if it's the most efficient approach, but it is pretty neat.
On a platform with 32-bit ints and it goes like this:

struct div_result {
unsigned int quotient;
unsigned int remainder;
};

#define FIVE_INVERSE 3435973837u
struct div_result divide10(unsigned int x) {
struct div_result result;
result.remainder = x & 1;
x >>= 1;
unsigned int xdiv4 = x >> 2;
result.quotient = x * FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2; x--;
result.quotient = x * FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2; x--;
result.quotient = x * FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2; x--;
result.quotient = x * FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2; x--;
result.quotient = x * FIVE_INVERSE;
return result;
}

I've tested this function on all 2^32 values of an unsigned int. It
effectively works by searching for the nearest number <= x/2 which is
divisible by 5 and then divides it by 5 by multiplying by the inverse of
5 in the group Z(2^32). It knows when it worked because otherwise it
gets a big result exceeding x/8.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #4
Derrick Coetzee wrote:
I came up with a custom algorithm for doing unsigned 32-bit division of
x by 10 using only a couple shifts and x % 5 hardware multiplications.


Wait - I'm an idiot. I only need ONE hardware multiplication! Here's the
revised algorithm:

struct div_result {
unsigned int quotient;
unsigned int remainder;
};

#define FIVE_INVERSE 3435973837u
struct div_result divide10(unsigned int x) {
struct div_result result;
result.remainder = x & 1;
x >>= 1;
unsigned int xdiv4 = x >> 2;
result.quotient = x * FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
return result;
}

Once again tested on all unsigned 32-bit integers.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #5
Derrick Coetzee wrote:
result.quotient = x * FIVE_INVERSE;


As an aside, on platforms with no hardware multiply, we can exploit the
regular bit pattern of FIVE_INVERSE to write this statement like this:

unsigned int temp = (x << 3) + (x << 2);
temp = (temp << 4) + temp;
temp = (temp << 8) + temp;
temp = (temp << 16) + temp;
result.quotient = temp + x;

The final program is then this, with 6 shifts, 1 AND, and 5-13 adds:

struct div_result {
unsigned int quotient;
unsigned int remainder;
};

#define FIVE_INVERSE 3435973837u
struct div_result divide10(unsigned int x) {
struct div_result result;
unsigned int xdiv4, temp;
result.remainder = x & 1;
x >>= 1;
xdiv4 = x >> 2;
temp = (x << 3) + (x << 2);
temp = (temp << 4) + temp;
temp = (temp << 8) + temp;
temp = (temp << 16) + temp;
result.quotient = temp + x;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
if (result.quotient <= xdiv4) return result;
result.remainder += 2;
result.quotient -= FIVE_INVERSE;
return result;
}

Once again, exhaustively tested.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #6
Derrick Coetzee <dc****@moonflare.com> writes:
I came up with a custom algorithm for doing unsigned 32-bit
division of x by 10 using only a couple shifts and x % 5
hardware multiplications. I'm not sure if it's the most
efficient approach, [...]


I doubt it. The book _Hacker's Delight_ describes a general
algorithm for unsigned division by a constant that only requires
one multiplication and a little error correction, typically ten
or so cycles on a RISC processor.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org
Nov 14 '05 #7
Ben Pfaff wrote:
Derrick Coetzee <dc****@moonflare.com> writes:
I came up with a custom algorithm for doing unsigned 32-bit
division of x by 10 using only a couple shifts and x % 5
hardware multiplications. I'm not sure if it's the most
efficient approach, [...]


I doubt it. The book _Hacker's Delight_ describes a general
algorithm for unsigned division by a constant that only requires
one multiplication and a little error correction, typically ten
or so cycles on a RISC processor.


My approach also requires only one multiplication (see my reply updates
to myself) and doesn't require a 64-bit product like the one you cite.
On the other hand, my approach doesn't scale to division by larger
constants very well.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #8
Derrick Coetzee wrote:
I doubt it. The book _Hacker's Delight_ describes a general
algorithm for unsigned division by a constant that only requires
one multiplication and a little error correction, typically ten
or so cycles on a RISC processor.


My approach also requires only one multiplication (see my reply updates
to myself) and doesn't require a 64-bit product like the one you cite.
On the other hand, my approach doesn't scale to division by larger
constants very well.


Er, although now that I look it also describes some other algorithms
that don't require a 64-bit product:

http://www.hackersdelight.org/divcMore.pdf

Thanks for the reference.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Nov 14 '05 #9
Alex Fraser wrote:
"sudi" <su**********@vsnl.net> wrote in message
I face the task of converting a 6 byte integer (MSB first) to a
decimal string. The platform I use supports only 4 byte basic
data type. I have written some algorithms that loops to many
times :-(. It would be great help if any of you can share a
high performance algorithm to do this.


If the platform can do 32-bit division (32-bit dividend and
divisor to 32-bit quotient and remainder) in hardware, then the
quickest way is probably to start by dividing the 48-bit number
by 100000 in software. The remainder from that division contains
the least significant five digits, and the quotient (which is
guaranteed to fit in 32 bits) contains the most significant
digits. Then use the hardware divide to extract the digits from
each part (dividing by 10).

If there is no hardware divide, I don't know of any better way
than the obvious repeated division by 10. You could perhaps get
some improvement by reducing the size of the division as you go.


A general solution to this problem, quite old, is to convert to
BCD via the hoary old double/dabble algorithm. It is all
explained in:

<http://cbfalconer.home.att.net/download/dubldabl.txt>

This is an algorithm, so discussion belongs on comp.programming.
However discussion of a C implementation belongs here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
9
by: franzkowiak | last post by:
Hello, I've read some bytes from a file and just now I can't interpret 4 bytes in this dates like a real value. An extract from my program def l32(c): return ord(c) + (ord(c)<<8) +...
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
8
by: steve | last post by:
I'd like to take the value of an int and put the value into a byte array. Basically the hexidecimal representation of an integer, with out having to convert the integer to a string and then to a...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
8
by: Nik Martin | last post by:
If I receive a message from the .net sockets class, it's a byte array. The original message is an ASCII string,like "70,70,70,70,70,0,0,0,0". The commas here represent individual bytes. The 70's...
5
by: Robin Tucker | last post by:
I need to marshal an IntPtr (which I've got from GlobalLock of an HGLOBAL) into a byte array. I know the size of the array required and I've got a pointer to the blob, but I can't see how to copy...
2
by: Jose Perez | last post by:
Dear All, I have the following problem. I am passing an encrypted value on the querystring from one aspx to another. Originally I was outputing characters such as "+, /,..." (which caused...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
6
by: =?Utf-8?B?TWljaGFlbA==?= | last post by:
Hi, I need to create a formatted byte array for SMPP, e.g.: 00 00 00 21 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is the length of the entire...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.