Converting large byte array to numeric string using Objective C - objective-c

I am facing a issue in which I need to convert a byte array of roughly the size in range of 20 - 28 bytes into a printable number string. In Java I am able to do it easily using BigInteger Class. In Objective C, the biggest type is Long Long Int which is only 64 bits. Please guide me the approach to solve the problem. I am not good in C language but some examples will be highly helpful. I must admit, I would be in the same problem in Java if the BigInteger Class was not there in Java as well.

Check this source:
http://www.santsys.com/code/display?folder=Objective-C&file=BigInt.h
EDIT:
Check the BitInt class, a big integer implementation in Objective-C, so you do not need to use C to solve your problem, and it's methods are similar to that of Java.

Related

Kotlin Primitives: How to reinterpret ByteArrays as the bits of primitives in Common Multiplatform code?

TL;DR is there an equivalent of C++'s reinterpret_cast<[primitive]>(bytes) for Kotlin Multiplatform?
Basically, what I am looking for is the following functionality:
You have a ByteArray with length, let's say, 4 bytes. The contents are
00 00 00 2A
Then there should be some function or operator to reinterpret these 4 bytes as an Int:
asInt(byteArrayOf(0x00, 0x00, 0x00, 0x2A))
Ideally, there would also be a way to control the Endian-ness of this operation. And most importantly, I'd want this to work on all available platforms (JVM, JS, Native). The question is: Is there such an operation?
Currently, I am doing the following:
For the integral types (Byte, Short, Int, Long), I use SHL / OR to construct the actual primitive. But this is of course not as efficient as just reinterpreting (maybe also copying) the value as the primitive value, since the bits are already in the right configuration
For the floating-point values, I'm still struggling: So far, I have not found a platform-independent solution, so I use expect / actual. I've found solutions for JVM and JS already (although not that satisfying), but I still don't know how to do it on Native. If it turns out that there is no common solution to the whole problem, I'd be very thankful if someone could point me towards a solution just for Float / Double converting on Native.
Thank you very much!
Your existing solution for integral types is likely to be ideal for multiplatform. For floating point values, you can convert them to and from int/long with
Double/Float.fromBits and Double/Float.toRawBits, and then use your existing solution for integral types.

Is there a way to create an sjcl.js bignum (bn) from an integer string?

sjcl.js provides codecs for reading in hex strings and utf8Strings, but it doesn't provide a codec for reading in base-10 integer strings. I'm trying to read in private keys for ECC point generation that are from another program (going from codec to bitArray then from bitArray to bn). Seems like an odd oversight which leads me to believe it is in there somewhere, just missing it.
Thanks,
PeterT

how to save more then 20 digit number in an integer in objective c Xcode any soluction

i am develop a game in Xcode and my game score increase 20 digit like this(1000000000000000000000000) how to manage it help me.i want to store 20 digit number in integer and want to increase and decrease value in objective c
Apple provide the NSDecimal value type, and an object wrapped version of it NSDecimalNumber. This is a floating-point type with a precision of 38 decimal digits, so it can easily hold your 20 digit integer and do arithmetic on it.
If you'd rather stick with "pure" integers then a 128-bit integer type will more than suffice - it will represent 38 digit decimals. This is an optional type in C, __int128 and unsigned __int128, and though the current Clang/Xcode compilers have the type there appears to be no built-in way to convert to/from strings for I/O - but converting an integer to a string is a simple algorithm you can implement yourself.
Another integer option is hidden inside of CFNumber which internally uses 128 bit integers. Apple release this as open source, CFNumber.c, and there are functions in there for addition, negation and conversion to text.
HTH

Converting in Smalltalk VisualWorks 7.9.1

I need to convert a ByteString to a Float32 (Exactly a 32-bit big-endian IEEE 754 floating point number). The ByteString is a part of an open sound control stream, received via UDP client.
I've spent a lot of time researching, so I'd like of someone handy with Smalltalk could give me a solution.
Thanks, in advance.
Since you seem to be receiving binary data, and not a decimal number in formatted ASCII, I would not recommend to call it ByteString, but rather ByteArray, Strings are an abstraction for containing characters, not bits.
In the case of VisualWorks, there is a class called UninterpretedBytes specialized in storing raw data (bits or rather bytes) for later interpretation.
This class has all the message you need to interpret the bytes, like for example #floatAt:bigEndian:
| yourBinaryStream buffer |
yourBinaryStream := ... insert some code to create your stream here...
buffer:= UninterpretedBytes from: (yourBinaryStream next: 4).
nextFloat := buffer floatAt: 1 bigEndian: true
In Pharo Smalltalk you can do:
(Float readFrom: '4.2') asIEEE32BitWord
readFrom: just reads a float from a string, and then you convert it to IEEE 754...
In VisualWorks you need to use the superclass method readFrom: as implemented in class Number.
First create a readstream on the string, for example:
Number readFrom: '192843.887' readStream

How to convert int to big-endian 32-bit number in Objective C?

I'm trying to implement WebSocket handshake (76) in ObjC and can't figure out (as I'm new to C and ObjC) how can I get the following:
List item
I've got two number
155712099
173347027
I need to convert them to 32 bit big-endian numbers
concatenate them into a string
and express the resulting string in ASCII
Help is highly appreciated.
Since you're using the iPhone SDK, you have access to Core Foundation. Use:
uint32_t myInt32Value = ...;
uint32_t myInt32AsABigEndianNumber = CFSwapInt32HostToBig(myInt32Value);
Objective-C is based on C and usually has the C standard library available, so you can use the standard htonl function. You can then do the string manipulation using the normal string functions.