Writing Hex To Console Using NSString - objective-c

I'm trying to write all the hex values, from 0 - 255 in a for loop. It's not printing in hex. I think the %x is what is causing the issue.
int i=0;
for (i = 0; i < 256; i++)
{
[_d writeXYZ:[NSString stringWithFormat:#"%x", i]];
}
Does anyone know the proper syntax to display 0-255 in hex?
When I do it like this:
int i=0;
for (i = 0; i < 256; i++)
{
NSLog(#"%x", i);
}
It runs perfectly, so it looks like my issue is within NSString.

Related

Populating table causes unexpected results

I am totally stumped. I have been debugging this for hours. I am allocating a table of 100 UInt32s by 100. I am loading a table of values and writing them to the 2D array. For some bizarre reason when I get to row 67, column 0 the writes appear to wrap back around to row 0 element 0.
I have rewritten it to allocate a list of arrays rather than a single malloc. Same exact behavior. I have tried doing math for the index: _map[row * 100 + column] instead of _map[i,j] and that leads to other strange behavior. I was thinking maybe something is overflowing, but I can't see how since the data is so small. Obviously I am doing something stupid but I just... can't.. see it.
Code snippet:
_map = malloc(100 * 100 * sizeof(UInt32));
int i = 0;
for (i=0; i <_columns; i++)
{
columnList = [[lineList objectAtIndex:i] componentsSeparatedByString:#","];
int j = 0;
for (j=0; j < _rows; j++)
{
UInt32 dataInt = atoi([[columnList objectAtIndex:j] UTF8String]);
// Convert the data
NSDictionary* tDict = [fileMap objectForKey:[NSString stringWithFormat:#"%i", dataInt]];
int newVal = [[tDict objectForKey:#"convert"] integerValue];
_map[i,j] = (UInt32)newVal;
UInt32 y = _map[i,j];
// This trips at row 67 element 0
if (_map[0,0] != 1)
printf("Here\n");
}
}
Any help would be absolutely most awesomely appreciated.
As I mention below, this code gives the same problem in that it corrupts the first line. As if every row is the same row:
int** testMap = malloc(100 * 100 * sizeof(int));
int data = 0;
for (int i = 0; i<100; i++)
{
for (int j = 0; j < 100; j++)
{
testMap[i, j] = data;
data++;
printf("(%i, %i)", i,j);
}
printf ("\n");
}

Adding together vector array functions

This code works as intended, giving me the number from these 10 input fields. Now, I want them all added together to one number. (all the miTimesFrac results.) Any really effective way of doing this?
double fraction[10] = {methane, ethane, propane, nbutane, ibutane,npentane,ipentane,nhexane,nitrogen,oxygen};
double Mi[10] = {16.0426,30.064,44.0962,558.123,558.123,72.1498,72.1498,86.1766,28.0134,31.9988};
double miTimesFrac[10];
for (int i=0; i<10; ++i) miTimesFrac[i] = Mi[i] * fraction[i];
I've tried a few things, like this
double TMiTimesFrac;
for (int i=1; i<10; ++i) TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
This returns the same value every time tho. Any functions i'm missing?
This:
double TMiTimesFrac;
for (int i=1; i<10; ++i)
TMiTimesFrac = miTimesFrac[i]+miTimesFrac[i-1];
Doesn't do the job, because you don't keep track of all the previous values of TMiTimesFrac, so that only the last value (miTimesFrac[9]+miTimesFrac[8]) contributes to the final value of TMiTimesFrac.
So first of all initialize TMiTimesFrac to zero. Then add every value to it:
double TMiTimesFrac= 0.0;
for(unsigned int i=0; i<10; i++)
{
TMiTimesFrac+=miTimesFrac[i];
}
Now you should be able to do it without pasting the code. I suggest to try it.

Converting NSString to array of chars inside For Loop

I'm trying to use an existing piece of code in an iOS project to alphabetize a list of words in an array (for instance, to make tomato into amoott, or stack into ackst). The code seems to work if I run it on its own, but I'm trying to integrate it into my existing app.
Each word I want it to alphabetize is stored as an NSString inside an array. The issue seems to be that the code takes the word as an array of chars, and I can't get my NSStrings into that format.
If I use string = [currentWord UTFString], I get an error of Array type char[128] is not assignable, and if I try to create the char array inside the loop (const char *string = [curentWord UTF8String]) I get warnings relating to Initializing char with type const char discards qualifiers. Not quite sure how I can get around it – any tips? The method is below, I'll take care of storing the alphabetized versions later.
- (void) alphabetizeWord {
char string[128], temp;
int n, i, j;
for (NSString* currentWord in wordsList) {
n = [currentWord length];
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (string[i] > string[j]) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
NSLog(#"The word %# in alphabetical order is %s", currentWord, string);
}
}
This should work :
- (void)alphabetizeWord {
char str[128];
for (NSString *currentWord in wordList)
{
int wordLength = [currentWord length];
for (int i = 0; i < wordLength; i++)
{
str[i] = [currentWord characterAtIndex:i];
}
// Adding the termination char
str[wordLength] = 0;
// Add your word
}
}
EDIT : Sorry, didn't fully understand at first. Gonna check this out.

Print C++ array in Objective-C "NSLog"

I'm trying to output and C++ Array (int inverse[3]) using NSLog, but If I try this way:
NSLog([NSString stringWithFormat:#"%d", inverse]);
It just dont work, But if I try like this:
NSLog([NSString stringWithFormat:#"%d", inverse[0]]);
I get the right output.
My objective is to get the whole array outputed.
Any ideas? Thanks
Use a for loop to print all the values.
for (int i=0; i<3; i++) {
NSLog(#"%i", inverse[i]);
}
or:
NSLog(#"%i, %i, %i", inverse[0], inverse[1], inverse[2]);
There is no need need to convert for string format conversion. You can print like these -
for ( int i=0; i<3; ++i )
NSLog(#"%i", inverse[i]);

Objective-C - Loop played but loop condition is false

I'm trying to convert a sectionned table into a flat list using this function into didSelectRowAtIndexPath (I have a NSArray that is initialisated with the number of items contained in each section) :
Somewhere... :-)
self.sectionsArray = [NSArray arrayWithObjects:macroIntNumber(1), macroIntNumber(3), macroIntNumber(12), nil];
then into didSelectRowAtIndexPath :
int selectedRow = 0;
int a = indexPath.section;
for (int i=0; i<indexPath.section-1; i++) {
selectedRow += [[self.sectionsArray objectAtIndex:i] intValue];
}
selectedRow += indexPath.row;
But... This crashes for indexPath.section = 0 (first section).
Because the loop is played infinitly until crash of the NSArray call...
Strange !!!
forcing for (int i=0; i<0-1; i++) { works
forcing for (int i=0; i<a-1; i++) { works
What am I missing ?
section is an NSUInteger, so it's unsigned. Thus, subtracting 1 from 0 on that unsigned integer is taking you to a very large number rather than -1.
It works when using a, because you've declared a as an int. :)