Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
Beam Positoning Monitor - Software
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
image/svg+xml
Discourse
Discourse
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Projects
Beam Positoning Monitor - Software
Commits
3e946b50
Commit
3e946b50
authored
Jul 17, 2014
by
Lucas Russo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
hal/hal_utils/*: add more generic string conversion functions
parent
6d0ada44
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
7 deletions
+44
-7
hal_utils.c
hal/hal_utils/hal_utils.c
+24
-5
hal_utils.h
hal/hal_utils/hal_utils.h
+20
-2
No files found.
hal/hal_utils/hal_utils.c
View file @
3e946b50
...
...
@@ -35,21 +35,32 @@
CHECK_HAL_ERR(err, HAL_UTILS, "[halutils]", \
halutils_err_str (err_type))
uint32_t
hex_to_str_len
(
uint32_t
key
)
uint32_t
num_to_str_len
(
uint32_t
key
,
uint32_t
base
)
{
uint32_t
i
=
0
;
uint32_t
rem
=
key
;
do
{
key
>>=
4
;
rem
/=
base
;
++
i
;
}
while
(
key
>
0
);
}
while
(
rem
>
0
);
return
i
;
}
char
*
halutils_stringify_key
(
uint32_t
key
)
uint32_t
hex_to_str_len
(
uint32_t
key
)
{
return
num_to_str_len
(
key
,
16
);
}
uint32_t
dec_to_str_len
(
uint32_t
key
)
{
return
num_to_str_len
(
key
,
10
);
}
char
*
halutils_stringify_key
(
uint32_t
key
,
uint32_t
base
)
{
uint32_t
key_len
=
hex_to_str_len
(
key
)
+
1
;
/* +1 for \0 */
uint32_t
key_len
=
num_to_str_len
(
key
,
base
)
+
1
;
/* +1 for \0 */
char
*
key_c
=
zmalloc
(
key_len
*
sizeof
(
char
));
ASSERT_ALLOC
(
key_c
,
err_key_c_alloc
);
...
...
@@ -64,4 +75,12 @@ err_key_c_alloc:
return
NULL
;
}
char
*
halutils_stringify_dec_key
(
uint32_t
key
)
{
return
halutils_stringify_key
(
key
,
10
);
}
char
*
halutils_stringify_hex_key
(
uint32_t
key
)
{
return
halutils_stringify_key
(
key
,
16
);
}
hal/hal_utils/hal_utils.h
View file @
3e946b50
...
...
@@ -10,7 +10,25 @@
#include <inttypes.h>
uint32_t
num_to_str_len
(
uint32_t
key
);
char
*
halutils_stringify_key
(
uint32_t
key
);
/* Returns the necessary string length including the termianting null character
* for a number in a arbitrary base */
uint32_t
num_to_str_len
(
uint32_t
key
,
uint32_t
base
);
/* Returns the necessary string length including the termianting null character
* for a hexadecimal number */
uint32_t
hex_to_str_len
(
uint32_t
key
);
/* Returns the necessary string length including the termianting null character
* for a decimal number */
uint32_t
dec_to_str_len
(
uint32_t
key
);
/* Allocates a string with the necessary size to fit a number in an arbitrary base */
char
*
halutils_stringify_key
(
uint32_t
key
,
uint32_t
base
);
/* Allocates a string with the necessary size to fit a decimal key */
char
*
halutils_stringify_dec_key
(
uint32_t
key
);
/* Allocates a string with the necessary size to fit an hexadecimal key */
char
*
halutils_stringify_hex_key
(
uint32_t
key
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment