473,320 Members | 2,071 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,320 software developers and data experts.

converting strings to numbers and back (large endian)

Subsciber123
I have a couple of short functions that I wrote in Python as part of a hash algorithm, and now I want to implement it in C++. I already have found an arbitrary precision integer library, but I don't know enough about C++ to know how to implement these two functions. Anyway, here is the equivalent code in Python:

Expand|Select|Wrap|Line Numbers
  1. def strToInt(string): # big endian
  2.     ret=0
  3.     for byte in string:
  4.         ret<<=8
  5.         ret+=ord(byte)
  6.     return ret
  7.  
  8. def intToStr(integer): # big endian
  9.     ret=""
  10.     while 0<integer:
  11.         integer,r=divmod(integer,256)
  12.         ret=chr(r)+ret
  13.     return ret
  14.  
Sep 7 '08 #1
4 1308
After some research into C++, I have managed to implement the first of the two functions, and it looks as though I can still implement the hash without the other (and probably a bit more efficiently, at that). Anyway, if anybody has suggestions on how I can improve the function, please tell me! I tested it, and it is functional, at least.

Expand|Select|Wrap|Line Numbers
  1. void strToInt(const char* str,int len,long& num) // big endian
  2. {
  3.     num=(long)0;
  4.     for(int i=0;i<len;++i)
  5.     {
  6.         num<<=8;
  7.         num+=(long)str[i];
  8.     }
  9. }
Keep in mind that the long type is arbitrarily long, thanks to "#define long RossiBigInt"

PS: Why did thescripts get rid of syntax highlighting?
Sep 7 '08 #2
arnaudk
424 256MB
PS: Why did thescripts get rid of syntax highlighting?
See this thread. .
Sep 7 '08 #3
See this thread. .
That's a bummer :(
I hope they fix the problem soon.
Sep 7 '08 #4
Oh, also:
If anybody can give me any hints as to how to do the inverse of the function that I wrote in C++, that would be appreciated. The problems I have include not knowing how long the string should be and the inability to cast a RossiBigInt to a char. Keep in mind that this should be large endian, as that is what I have defined in the standard for my hash.

If anybody wants to see the hash that I wrote (the Python version) PM me.
Sep 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: i_vincent | last post by:
Hi all, Newbie Python programmer here, so please be patient. I have spent all day googling for an answer to my problem, but everything I try fails to work (or works from the Interpreter with a...
0
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
2
by: Mehta Shailendrakumar | last post by:
Hi, I am sending this question again as new question rather than reply to old question Please refer below: struct raw_data { unsigned char x; unsigned char y; }; union full_data
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
5
by: srikant | last post by:
I am writing a client in C# that needs to communicate over the network to a legacy C++ application that uses Unicode strings. I realize that C# strings are already in Unicode, however, how do I...
3
by: Pete Davis | last post by:
I've never done this in C# so I don't know what the appropriate way of doing it is. I've got an array of bytes and I need to convert the array into "usable" data. For example, the first 4 bytes...
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.